Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

question from:https://stackoverflow.com/questions/65925554/file-upload-problems-with-php-copy-files-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
263 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...