ra_ie_darkness Posted March 19, 2012 Share Posted March 19, 2012 I'm using xampp on windows vista I am a beginner in php and am working on creating a file hosting website got a form to upload the file from the client in uploadform.php <html> <body> <form method="post" action="uploadfile.php" enctype="multipart/form-data"> <input type="file" name="usr" id="usr"/><br /> <input type="submit" value="Upload"/> </form> </body> </html> the file handling code is in uploadfile.php <?php class upload { function fupload() { $dir = "C:\\xampp\htdocs\learning\fileshare database.txt"; echo "My name is: ".$_FILES['usr']['name']."<br/>"; echo "Location ".$_FILES['usr']['tmp_name']."<br/>"; echo "Size:".($_FILES['usr']['size'] / 1024)." kb <br/>"; echo "File type".$_FILES['usr']['type']."<br/>"; if(file_exists("htdocs/".$_FILES['usr']['name'])) { echo $_FILES['usr']['name']."already exists"; } else { copy($_FILES['usr']['name'],$dir); echo "File moved"; } } } $up = new upload(); $up->fupload(); ?> The problem is that it does not copy the file from the tmp directory to the specified one the error i get is: Warning: copy(fileshare databse.txt) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\learning\uploadfile.php on line 20 I am facing the same problem if i try move_uploaded_file () function Could any one help with this issue, thanks Quote Link to comment https://forums.phpfreaks.com/topic/259265-file-handling-issues/ Share on other sites More sharing options...
DancingNapkin Posted March 19, 2012 Share Posted March 19, 2012 the error i get is: Warning: copy(fileshare databse.txt) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\learning\uploadfile.php on line 20 Your code specifically defines $dir as $dir = "C:\\xampp\htdocs\learning\fileshare database.txt". If you're receiving that error, then you need to change the spelling in your file name. Quote Link to comment https://forums.phpfreaks.com/topic/259265-file-handling-issues/#findComment-1329098 Share on other sites More sharing options...
ra_ie_darkness Posted March 19, 2012 Author Share Posted March 19, 2012 Thanks, i will rename the file but does that mean there is no problem in my code other than the file name is there a problem if there is a space in the file name if so then how will i solve this problem considering a user wants to upload a file that has spaces in its name Quote Link to comment https://forums.phpfreaks.com/topic/259265-file-handling-issues/#findComment-1329106 Share on other sites More sharing options...
ra_ie_darkness Posted March 19, 2012 Author Share Posted March 19, 2012 changing the file name did not work still getting the same warning message Quote Link to comment https://forums.phpfreaks.com/topic/259265-file-handling-issues/#findComment-1329146 Share on other sites More sharing options...
DavidAM Posted March 19, 2012 Share Posted March 19, 2012 copy($_FILES['usr']['name'],$dir); The name element of the FILES array is the basename (filename without the path) of the file that the user uploaded. The tmp_name element is the filename the server used to temporarily store the uploaded file. This temporary file is stored in a directory somewhere on the server (not in your path). The move_uploaded_file function knows where this file is stored, which is one of the reasons you should use that function instead of copy. The chances are very very high that the tmp_name is not the same as the name. Quote Link to comment https://forums.phpfreaks.com/topic/259265-file-handling-issues/#findComment-1329156 Share on other sites More sharing options...
ra_ie_darkness Posted March 19, 2012 Author Share Posted March 19, 2012 okay i am now using move_uploaded_file here is how it looks like <?php class upload { function fupload() { echo "My name is: ".$_FILES['usr']['name']."<br/>"; echo "Location ".$_FILES['usr']['tmp_name']."<br/>"; echo "Size:".($_FILES['usr']['size'] / 1024)." kb <br/>"; echo "File type".$_FILES['usr']['type']."<br/>"; if(file_exists("htdocs/".$_FILES['usr']['name'])) { echo $_FILES['usr']['name']."already exists"; } else { move_uploaded_file($_FILES['usr']['tmp_name'],"/learning/".$_FILES['usr']['name']); echo "File moved"; } } } $up = new upload(); $up->fupload(); ?> is it possible that i am not using the function properly? learning is a folder inside htdocs(xampp) now I am getting this message Warning: move_uploaded_file(/learning/) [function.move-uploaded-file]: failed to open stream: Invalid argument in C:\xampp\htdocs\learning\uploadfile.php on line 19 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\phpA56E.tmp' to '/learning/' in C:\xampp\htdocs\learning\uploadfile.php on line 19 Quote Link to comment https://forums.phpfreaks.com/topic/259265-file-handling-issues/#findComment-1329159 Share on other sites More sharing options...
DavidAM Posted March 19, 2012 Share Posted March 19, 2012 move_uploaded_file($_FILES['usr']['tmp_name'],"/learning/".$_FILES['usr']['name']); The leading slash on your destination "/learning/" indicates the root of the file system. This would be equivalent to specifying "C:\learning\...". This is most likely NOT where you are trying to put the file. The error message is indicating that that directory does not exist. You need to either take off the leading slash --- which will put it in the "current" directory, wherever that is --- OR (the preferred approach) define a variable that holds the destination directory name: Using parts from your original post: $dir = "C:\\xampp\\htdocs\\learning\\"; // ... checking and processing code move_uploaded_file($_FILES['usr']['tmp_name'], $dir . $_FILES['usr']['name']); Quote Link to comment https://forums.phpfreaks.com/topic/259265-file-handling-issues/#findComment-1329172 Share on other sites More sharing options...
ra_ie_darkness Posted March 20, 2012 Author Share Posted March 20, 2012 that worked, thank you Quote Link to comment https://forums.phpfreaks.com/topic/259265-file-handling-issues/#findComment-1329357 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.