0
$targetFolder = '/LP/media/image'; // Relative to the root

if (!empty($_FILES)) {
    $tmpName = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
    //$ext = end(explode('.', $_FILES['Filedata']['name']));
    $targetFile = rtrim($targetPath, '/') . '/' . $_FILES['Filedata']['name'];

    // Validate the file type
    echo $targetFile;
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    $fileTypes = array('jpg', 'jpeg', 'png','JPG','JPEG','PNG'); // File extensions
    if (in_array($fileParts['extension'], $fileTypes)) {
        move_uploaded_file($tmpName, $targetFile);
        //echo '1'.$fileParts['filename'];
        // echo $fileParts['filename'];

        $aimage = file_get_contents($tmpName);

       // echo '<img src="data:image/jpeg;base64,' . base64_encode($aimage) . '" width=\'100\' height=\'100\' />';
    } else {
        echo 'Invalid file type.';
    }
}
?>

Same code is working on my local machine, but deployed on remote server. Then getting below error.

/home/username/public_html/LP/media/image/default_avatar.png
Warning: move_uploaded_file(/home/username/public_html/LP/media/image/default_avatar.png): failed to open stream: No such file or directory in /home/username/public_html/uploadify/gallery.php on line 28

Warning: move_uploaded_file(): Unable to move '/tmp/phpoe0fBd' to '/home/username/public_html/LP/media/image/default_avatar.png' in /home/username/public_html/uploadify/gallery.php on line 28

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • 1
    the directory you want to move to doesn't exist on the server – Gouda Elalfy Feb 29 '16 at 14:14
  • It is there. I have created. – Ravi Feb 29 '16 at 14:15
  • 3
    Check for the permissions. – Prerak Sola Feb 29 '16 at 14:16
  • does your HTML form use a post method and a valid enctype? with same name attribute? – Funk Forty Niner Feb 29 '16 at 14:17
  • I'm able to access and permission is set to 0777. – Ravi Feb 29 '16 at 14:17
  • I'm using uploadify library. – Ravi Feb 29 '16 at 14:19
  • make sure for folder name..they are case sensitive – Jimish Gamit Feb 29 '16 at 14:19
  • ^ if on a * NIX systetm. – Funk Forty Niner Feb 29 '16 at 14:20
  • @Fred-ii- I didn't get it. What should I make change in my code ? – Ravi Feb 29 '16 at 14:22
  • 1
    Probably nothing. It's the HTML form that I was talking about, in regards to name attribute and a valid enctype. I don't know uploadify enough to further comment, but uploading files requires a valid enctype and a POST method. Someone mentioned about folder lettercase. As I said, below it, that only applies if you're on UNIX/LINUX. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Feb 29 '16 at 14:24
  • I have same code to upload image on database except few different code, which saves file to folder and that is working. Let me try what you suggested. – Ravi Feb 29 '16 at 14:27
  • 1
    could also check to see what the upload max size is allowed on the server – Funk Forty Niner Feb 29 '16 at 14:27
  • @Fred-ii- you should said "In all operating systems except one from Hasefroch, Redmond". – Marcos Pérez Gude Feb 29 '16 at 14:29
  • 2
    @jWeaver `$targetFolder = '/LP/media/image';` on the server means it will look in the root of your filesystem because of the slash `/` the it will fail to find the directory. instead you should be using relative path. – meda Feb 29 '16 at 14:29
  • @meda I too was questioning that earlier and left a comment about it, but deleted it :( I should not have done that. – Funk Forty Niner Feb 29 '16 at 14:30
  • 1
    @Fred-ii- yeah at least OP should try to upload in the same directory where the script runs to make sure the php can upload – meda Feb 29 '16 at 14:32
  • @meda you may be correct. But, if you look at the error message. It was trying to upload file at the right path only `/home/username/public_html/LP/media/image/`. When I typed that path on the server I was at the same folder where I suppose to. – Ravi Feb 29 '16 at 14:32
  • ^ which is the reason why I deleted my comment about it, when seeing the path for it. – Funk Forty Niner Feb 29 '16 at 14:33
  • @jWeaver can you upload to the same folder? – meda Feb 29 '16 at 14:33
  • ok, now there's an answer below. – Funk Forty Niner Feb 29 '16 at 14:34
  • @jWeaver be sure all your folders LP, media, and image is 0777 permission – Gouda Elalfy Feb 29 '16 at 14:36
  • well gentlemen (and ladies if any)... sorry, but I am out of this loop – Funk Forty Niner Feb 29 '16 at 14:40
  • this may help you: [move_uploaded_file gives “failed to open stream: Permission denied ” error after all configurations i did](http://stackoverflow.com/questions/8103860/move-uploaded-file-gives-failed-to-open-stream-permission-denied-error-after), it basically points that the ownership of the uploaded file and the directory, where the file it's going to be copied are a common cause for these problems... – DIEGO CARRASCAL Feb 29 '16 at 14:45

4 Answers4

0

The code you provide isn't very clear to tell the source of the problem, so I will provide you with a full snippet where you can take better conclusions.

In this case, I sent an array from JavaScript to my PHP file using a formdata under the name "documents". I also modify the name of the file so it becomes: "filename_optional_$i(int)".

for($i=0; $i<count($_FILES['documents']['name']); $i++){
    list($dump, $extention) = explode(".", (string)$_FILES['documents']['name'][$i]);
    $document_name = $file_name.'_optionnel'.$i;
    if(($_FILES['documents']['size'][$i] < 500000)){
        if ($_FILES['documents']["error"][$i] > 0){
            echo 'Erreur.';
        }else{
            if(file_exists( $directory_path . $document_name . "." . $extention)) {
                echo 'Le fichier existe déjà.';
            }else{
                $sourcePath = $_FILES['documents']['tmp_name'][$i];
                $targetPath = $directory_path . $file_name ."/". $document_name . "." . $extention;
                array_push($attachments, $targetPath);
                move_uploaded_file($sourcePath, $targetPath);
            }
        }
    }else{
        echo 'Le fichier est trop grand.';
    }
}

Make sure your path variables are pointing to the correct folder. Say, for instance, if you want your file to go to the foldier images, you have to do:

path_to_folder/images/ and not path_to_folder/images

EDIT:

Here's how your filename and directory path variables should look like:

/*$nom, $prenom and $user_id come from database values*/
$file_name = $nom."_".$prenom."_".$user_id;

$directory_path = dirname( __FILE__ ) . '/user_docs/';

/* Create mask */ 
$oldmask = umask(0);

/* Create the folder for user */
$directory = mkdir($directory_path.$file_name, 0777);

/* redo the mask */
umask($oldmask);

I'm quite new to this site, but the least I can do for you is offer you this code I've done to help you out. I've written some comments to guide you through.

Full code source for upload

Iuri Dias
  • 97
  • 9
  • what should I do with this code ? How and where should i write this code ? What should I expect as an output ? – Ravi Feb 29 '16 at 14:37
  • This snippet is working as intended, you could simply take a look at it and compare it to yours, please take a look at the edit as it adds quite some curcial information. – Iuri Dias Feb 29 '16 at 14:39
  • *"Here's how my filename and directory path variables look like:"* - Yeah, but we're talking about what the OP's code should look like. Just because this works on "your" machine, doesn't mean it'll work on theirs. – Funk Forty Niner Feb 29 '16 at 14:39
0

I have this little function (not the best) but it "works" in my case the uploaded files must be moved or they get deleted, so I move them to a folder next to my PHP file and echo a link to it (you can them move it any where else, rename it or whatever:

function save_file() {
    $gs_name = $_FILES['uploaded_files']['tmp_name'];
    if(is_readable($gs_name) && $_FILES['uploaded_files']['error'] == 0) { //Tells whether a file exists and is readable. no error during upload (0)
        move_uploaded_file($gs_name, 'upload/'.$_FILES['uploaded_files']['name']);

        echo "saved.<a href='upload/".$_FILES['uploaded_files']['name']."' target='_blank'/>".$_FILES['uploaded_files']['name']."</a><br/>";
        echo "<p><a href='upload/' target='_self'>Uploaded files.</a></p>";
        echo "<p><a href='upload_file.php' target='_self'>Continue...</a></p>";
    }
}

The important part about it is this if:

if(is_readable($gs_name) && $_FILES['uploaded_files']['error'] == 0)

it allow it to check if the file can be copied and if there were no errors that could leave the file damage... I point at this because the path seems to be ok, other already mention the file and folder permissions.

DIEGO CARRASCAL
  • 1,999
  • 14
  • 16
0

Try the following code. It should be easy to identify the error:

<?php

$upload_dir = $_SERVER['DOCUMENT_ROOT'] . '/LP/media/image';

if (isset($_FILES['Filedata'])) {

    $file = $_FILES['Filedata'];

    if ($file['error'] !== UPLOAD_ERR_OK) {
        echo 'error: ', $file['error'], PHP_EOL;
        exit;
    }

    $allowed = array('jpg', 'jpeg', 'png');
    $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
    if (!in_array($extension, $allowed)) {
        echo 'error: invalid file extension', PHP_EOL;
        exit;
    }

    $target = $upload_dir . '/' . basename($file['name']);
    if (!move_uploaded_file($file['tmp_name'], $target)) {
        echo 'error: move_uploaded_file failed', PHP_EOL;
        print_r(error_get_last());
        exit;
    }

    echo 'success', PHP_EOL;
    echo $target, PHP_EOL;
}
vbarbarosh
  • 3,502
  • 4
  • 33
  • 43
0

whom still have this problem after tried all solutions suggested here.. they have to try this small thing is to respect the letter sensitive case of the name of folders.. folder "Upload" <> "upload".. thx