0
votes

Update

So I think I've tracked it down to the file not being available:

I've checked the ownership of the folders for uploads and for tmp (above doc root). Both seem to have the correct ownership as does the upload.php file

I've checked the $_FILES and it shows

        [name] => sampleImage3.jpg
        [type] => image/jpeg
        [tmp_name] => /tmp/phpypdbTb
        [error] => 0
        [size] => 19791

So the file is being loaded into the tmp folder

However when I try a

copy($_FILES['file']['tmp_name'],$uploadFile);

where

tmp_name is mnt/storage/vhosts/domain.com/tmp/phpypdbTb uploadFile is uploads/sampleImage3.jpg

it throws a

Warning: copy(uploads/sampleImage3.jpg): failed to open stream: No such file or directory

Any ideas how to debug and figure out what's going on?


Original

I want upload a file via ajax.

Html is

<div class="container">
    <form method="post" action="" enctype="multipart/form-data" id="myform">
        <div class='preview'>
            <img src="" id="img" width="100" height="100">
        </div>
        <div >
            <input type="file" id="file" name="file" />
            <input type="button" class="button" value="Upload" id="but_upload">
        </div>
    </form> </div>

JavaScript is

jQuery(document).ready(function(){

    jQuery("#but_upload").click(function(){

        var fd = new FormData();
        var files = jQuery('#file')[0].files;

        if(files.length > 0 ){
            fd.append('file',files[0]);

            jQuery.ajax({
                url: '/upload.php',
                type: 'post',
                data: fd,
                contentType: false,
                processData: false,
                success: function(response){
                    if(response != 0){
                        console.log(response);
                        //jQuery(".preview img").show(); // Display image element
                    }else{
                        alert('file not uploaded');
                    }
                },
            });
        }else{
            alert("Please select a file.");
        }
    });
});

which loads upload.php

<?php

if(isset($_FILES['file']['name'])){

    /* Getting file name */
    $filename = $_FILES['file']['name'];
    $location = "/uploads";
    $imageFileType = pathinfo($location,PATHINFO_EXTENSION);
    $imageFileType = strtolower($imageFileType);

/*    echo "<pre>";
    print_r($_FILES);
    echo "</pre>";
*/

    /* Valid extensions */
    $valid_extensions = array("jpg","jpeg","png","JPG");
    move_uploaded_file($_FILES['file']['tmp_name'],'$location/$filename');


    $response = 0;
    /* Check file extension */
  //  if(in_array(strtolower($imageFileType), $valid_extensions)) {
        /* Upload file */
        if(move_uploaded_file($_FILES['file']['tmp_name'],'$location/$filename')){
            $response = $location;
        }
    //}

    echo $response;

}

echo 0;

The ajax call works no problem and I can even print_r the FILES variable

Array ( [file] => Array ( [name] => Organ Pipes and SGW.jpg [type] => image/jpeg [tmp_name] => /tmp/phpQxJK34 [error] => 0 [size] => 373852 )

However the file won't upload. Any ideas what I'm doing wrong?

1
You appear to be calling move_uploaded_file twice. First time is after $valid_extensions is set. Second time is in the if right after $response is set to zero. Try commenting out that if block where the second move is.Dave
I removed the second move_upload_file and it made no differenceuser1616338
Make sure you have error reporting turned on as well as error logging so any errors will get logged. Since you're calling this from Ajax you wouldn't see any errors if they were happening.Dave

1 Answers

0
votes

Your location is an absolute path /uploads and I think you can find it like this

cd /uploads;
ls -la

And you should see it
But if upload directory is in your project or in the same place of your php file you need to change /uploads to ./uploads