Reputation: 393
I had a simplest, dumbest script for uploading files and returning success true or false after the operation. Not sure why, suddenly the script stopped working giving me a failed to open stream: No such file or directory
error in move_uploaded_file
line. It's strange since I haven't changed any paths, permissions, nothing basically. Any tips how to tackle this ? I'm a total newcomer to php. My script below :
if(isset($_FILES)){
$file_tmp = $_FILES['mpp-file']['tmp_name'];
$file_name = $_FILES['mpp-file']['name'];
if(is_uploaded_file($file_tmp)) {
if(move_uploaded_file($file_tmp, "tmp/$file_name")){
echo '{"success": true}';
unlink("tmp/$file_name");
} else {
echo '{"success": false}';
}
} else{
echo '{success: false}';
}
}
And the page from stacktrace :
<html>
<head></head>
<body>
<font size="1"><table class="xdebug-error" dir="ltr" border="1" cellspacing="0" cellpadding="1">
<tbody><tr><th align="left" bgcolor="#f57900" colspan="5"><span style="background-color: #cc0000; color: #fce94f; font-size: x-large;">( ! )</span> Warning: move_uploaded_file(tmp/MSP1.mpp) [<a href="function.move-uploaded-file">function.move-uploaded-file</a>]: failed to open stream: No such file or directory in K:\LAMP\www\project\msp-load.php on line <i>7</i></th></tr>
<tr><th align="left" bgcolor="#e9b96e" colspan="5">Call Stack</th></tr>
<tr><th align="center" bgcolor="#eeeeec">#</th><th align="left" bgcolor="#eeeeec">Time</th><th align="left" bgcolor="#eeeeec">Memory</th><th align="left" bgcolor="#eeeeec">Function</th><th align="left" bgcolor="#eeeeec">Location</th></tr>
<tr><td bgcolor="#eeeeec" align="center">1</td><td bgcolor="#eeeeec" align="center">0.0034</td><td bgcolor="#eeeeec" align="right">677944</td><td bgcolor="#eeeeec">{main}( )</td><td title="K:\LAMP\www\gantt_latest\examples\MSProject_import\msp-load.php" bgcolor="#eeeeec">..\msp-load.php<b>:</b>0</td></tr>
<tr><td bgcolor="#eeeeec" align="center">2</td><td bgcolor="#eeeeec" align="center">0.0034</td><td bgcolor="#eeeeec" align="right">678256</td><td bgcolor="#eeeeec"><a href="http://www.php.net/move_uploaded_file" target="_new">move_uploaded_file</a>
( )</td><td title="K:\LAMP\www\project\msp-load.php" bgcolor="#eeeeec">..\msp-load.php<b>:</b>7</td></tr>
</tbody></table></font>
<br>
<font size="1"><table class="xdebug-error" dir="ltr" border="1" cellspacing="0" cellpadding="1">
<tbody><tr><th align="left" bgcolor="#f57900" colspan="5"><span style="background-color: #cc0000; color: #fce94f; font-size: x-large;">( ! )</span> Warning: move_uploaded_file() [<a href="function.move-uploaded-file">function.move-uploaded-file</a>]: Unable to move 'K:\LAMP\tmp\phpC7A5.tmp' to 'tmp/MSP1.mpp' in K:\LAMP\www\project\msp-load.php on line <i>7</i></th></tr>
<tr><th align="left" bgcolor="#e9b96e" colspan="5">Call Stack</th></tr>
<tr><th align="center" bgcolor="#eeeeec">#</th><th align="left" bgcolor="#eeeeec">Time</th><th align="left" bgcolor="#eeeeec">Memory</th><th align="left" bgcolor="#eeeeec">Function</th><th align="left" bgcolor="#eeeeec">Location</th></tr>
<tr><td bgcolor="#eeeeec" align="center">1</td><td bgcolor="#eeeeec" align="center">0.0034</td><td bgcolor="#eeeeec" align="right">677944</td><td bgcolor="#eeeeec">{main}( )</td><td title="K:\LAMP\www\gantt_latest\examples\MSProject_import\msp-load.php" bgcolor="#eeeeec">..\msp-load.php<b>:</b>0</td></tr>
<tr><td bgcolor="#eeeeec" align="center">2</td><td bgcolor="#eeeeec" align="center">0.0034</td><td bgcolor="#eeeeec" align="right">678256</td><td bgcolor="#eeeeec"><a href="http://www.php.net/move_uploaded_file" target="_new">move_uploaded_file</a>
( )</td><td title="K:\LAMP\www\project\msp-load.php" bgcolor="#eeeeec">..\msp-load.php<b>:</b>7</td></tr>
</tbody></table></font>
</body>
</html>
I'm using WAMP server on Win7.
Upvotes: 1
Views: 4338
Reputation: 307
i had a similar issue, but to be frank my issue was with a pipe character in the filename. please check whether your filename has any special characters.
Upvotes: 0
Reputation: 1628
You are required to check whether the tmp directory exists or not. This error is displayed when the location where you are trying to move a file doesnot exists.
if(isset($_FILES)){
$file_tmp = $_FILES['mpp-file']['tmp_name'];
$file_name = $_FILES['mpp-file']['name'];
$directory_path = $_SERVER["DOCUMENT_ROOT"]."/tmp/$file_name";
if(is_uploaded_file($file_tmp)) {
if(is_dir($directory_path))
{
if(move_uploaded_file($file_tmp, "tmp/$file_name")){
echo '{"success": true}';
unlink("tmp/$file_name");
} else {
echo '{"success": false}';
}
}else
{
echo "No such directory exists";
}
} else{
echo '{success: false}';
}
}
Upvotes: 2
Reputation: 360562
Don't do:
if(isset($_FILES)){
failed uploads will STILL produce a $_FILES array. You need to check the error parameter:
if ($_FILES['mpp-file']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['mpp-file']['error']);
}
The error codes/constants are defined here: http://php.net/manual/en/features.file-upload.errors.php
As well, do NOT use the ['name']
parameter for storing the file. That field is under the control of the remote user, and they can trivially hack the upload data to say ../../../../../../../etc/passwwd
and your script will happily overwrite any file on the server the user wants.
Upvotes: 1