Here is my HTML code :
<form enctype="multipart/form-data" action="new1.php" method="POST">
Choose File: <input name="userfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
And this is my PHP code :
<?php
$path = "files/";
$path = $path.basename( $_FILES['userfile']['name']);
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $path)) {
echo "Success uploading". basename($_FILES['userfile']['name']);
} else{
echo "Error when uploading file.";
}
?>
Here are the errors I got when running on XAMPP
Warning: move_uploaded_file(files/ChicKissLove.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\phptest
ew1.php on line 7
Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpAF97.tmp' to 'files/ChicKissLove.jpg' in C:\xampp\htdocs\phptest
ew1.php on line 7
Error when uploading file.
Anyone can find out what's wrong ? I 'm unable to upload the files.
Create files directory in the same directory where your code is first. And You are good to go.
P.S next time save your files with a random name because someone else might upload a file with same name creating more problems and always filter your uploading files never allow executable files.
Create files
folder in the respective section or use below code, it will create directory automatically.
$path = "files/";
if(!is_dir($path)){
mkdir($path, 0777, true);
}
There might be two possibilities:
Either there is no directory with name files where the images could be stored
Or the path to the directory is wrong. the directory should be in the same place where your new1.php resides
and last but not the least
Note:-do not forget to give the required permissions to the directory