Jump to content

Recommended Posts

Im trying to use move_uploaded_file() to move an uploaded file from the temp directory to a different directory and when I run this function I get this error:

 

Warning: move_uploaded_file(/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phprQRBDe' to '/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf' in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238

There was an error uploading the file, please try again!

 

I have no idea why, below is my code:

 

<?php

$target_path = $uploadLocation . basename( $_FILES['upfile']['name']);
$ext = end(explode('.', $_FILES['upfile']['name']));


  function generatePassword ($length = 
  {

    // start with a blank password
    $password = "";

    // define possible characters - any character in this string can be
    // picked for use in the password, so if you want to put vowels back in
    // or add special characters such as exclamation marks, this is where
    // you should do it
    $possible = "_12346789abcdfghjkmnpqrtvwxyzABCDFGHJKLMNPQRTVWXYZ";

    // we refer to the length of $possible a few times, so let's grab it now
    $maxlength = strlen($possible);
  
    // check for length overflow and truncate if necessary
    if ($length > $maxlength) {
      $length = $maxlength;
    }

    // set up a counter for how many characters are in the password so far
    $i = 0; 
    
    // add random characters to $password until $length is reached
    while ($i < $length) { 

      // pick a random character from the possible ones
      $char = substr($possible, mt_rand(0, $maxlength-1), 1);
        
      // have we already used this character in $password?
      if (!strstr($password, $char)) { 
        // no, so it's OK to add it onto the end of whatever we've already got...
        $password .= $char;
        // ... and increase the counter by one
        $i++;
      }

    }

    // done!
    return $password;

  }
  
  
$filename = generatePassword();
$filename = $filename.".".$ext;

$filelocation = "/home/mikeh/public_html/uzErUpl0ds/".$filename;

$allowed = "doc docx rtf txt pdf xls";
$allow = explode (" ",$allowed);


if (in_array($ext,$allow)){

if(move_uploaded_file($_FILES['upfile']['tmp_name'], $filelocation)) {
//TODO: Insert date regex format and make the catagory selection auto//
$uid = $_POST['catagory'];
$name =$_POST['title'];
$author = $_POST['author'];
$cat = $_POST['code'];
$dt1=date("Y-m-d");
$size = filesize($filelocation); 
die($filelocation);

$query = "INSERT INTO newnotes (uid, name, catagory, location, author, date, filesize) VALUES ('$uid', '$name', '$cat', '$filename', '$author', '$dt1','size')";
//die ($query);
$res = mysql_query($query) or die (mysql_error());


    echo "The file: ".  basename( $_FILES['upfile']['name']).
    " has been uploaded! It will be reviewed and if possible you will be granted credits within the next 24 hours.";
} else{
    echo "There was an error uploading the file, please try again!";
}

}else{
echo "You have tried to upload an file type that we do not allow. The current allowed types of files are, ".implode(',',$allow).". If you are trying to upload a note with a diffrent file extension please use <a href=\"contact.php\">this form</a> to let us know so we can fix our website to allow this type of file in the future.";	
}

?>

Link to comment
https://forums.phpfreaks.com/topic/248218-move_uploaded_file-issue/
Share on other sites

Does your <form> tag include the enctype="multipart/form-data" attribute? Also, you aren't checking to see whether $_FILES['upfile']['error'] contains a value that would indicate an error occurred before attempting to move the file.

If the file size was an issue, it would show up in $_FILES['userfile']['error'].

 

UPLOAD_ERR_INI_SIZE

 

    Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

UPLOAD_ERR_FORM_SIZE

 

    Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

Warning: move_uploaded_file(/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phprQRBDe' to '/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf' in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238

 

You need to set the destination path to an actual folder that exists in the following statement -

 

$filelocation = "/home/mikeh/public_html/uzErUpl0ds/".$filename;

 

 

 

Pikachu2000, you are right IF a person adds $_FILES['userfile']['error'] to their page to get error number.    Otherwise they would see a message like was posted.

Warning: move_uploaded_file(/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phprQRBDe' to '/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf' in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238

There was an error uploading the file, please try again!

  See I posted here regarding a similar issue but received no replies. http://www.phpfreaks.com/forums/index.php?topic=342959.msg1617984#msg1617984

 

After searching the web I found the "$_FILES['userfile']['error']"  to give me the error number and then looked it up.  Until then, all I was seeing was the "failed to open stream" warning.

@Drummin, for what you are suggesting, the actual error messages you were getting would have indicated that the source file did not exist. Not that the destination folder did not exist. Not the exact same error messages, so not the exact same problem.

 

You cannot generalize that an error message that contains "failed to open stream" always means the same thing.

Pikachu2000, you are right IF a person adds $_FILES['userfile']['error'] to their page to get error number.    Otherwise they would see a message like was posted.

Warning: move_uploaded_file(/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phprQRBDe' to '/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf' in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238

There was an error uploading the file, please try again!

  See I posted here regarding a similar issue but received no replies. http://www.phpfreaks.com/forums/index.php?topic=342959.msg1617984#msg1617984

 

After searching the web I found the "$_FILES['userfile']['error']"  to give me the error number and then looked it up.  Until then, all I was seeing was the "failed to open stream" warning.

 

The form does have that attribute and $_FILES['image']['error'] equals 0

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.