0
<html>
<head>
<title>Zachs Page</title>
</head>
<body>

<form enctype="multipart/form-data" action="upload_image.php" method="post">
<input type="hidden" name="MAX_SIZE" size="524288">
<p>
File:<input type="file" name="upload"/></p>
<input type="submit" name="submit" value="submit"/>
<input type="hidden" name="submitted" value="TRUE"/>
</form>

<?php 

if(isset($_POST['submitted']))
{

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

$allowed= array('image/pjpeg',
'image/jpeg', 'image/jpeg',
'image/JPG', 'image/X-PNG',
'image/PNG', 'image/png',
'image/x-png');
if(in_array($_FILES['upload']['type'],$allowed))
{
if(move_uploaded_file($_FILES['upload']['tmp_name'] , "/MyFiles/{$_FILES['upload']['name']
}"))
{

echo "The File has been uploaded";
}
else
{
echo "Please upload a proper type.";
}

}

}

if($_FILES['upload']['error']>0)
{

switch($_FILES['upload']['error']){

case 1:
print 'the files exceeds max upload size setting in php.ini';
break;
case 2:
print 'Exceeds MAX_FILE_SIZE in html form';
break;
case 3:
print'The file was only partially uploaded';
break;
case 4:
print 'The file was not uploaded';
break;
case 6:
print 'No temp_file was available';
break;
case 7:
print 'Unable to write to disk';
break;
case 8:
print 'The file upload was stopped';
default:
print 'A system error has occured';
break;
}

}
if(file_exists($_FILES['upload']['tmp_name']&& is_file($_FILES['upload']['tmp_name'])))
{
unlink
($_FILES['upload']['tmp_name']);
}

}
?>
</body>
</html>

I have used a similar template before from Visual Quick Pro Guide and had no errors with this. But whenever I try to upload an image (that is quite small) I get

       Warning: move_uploaded_file(/MyFiles/4.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\MyFiles\upload_image.php on line 31


Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php1B3E.tmp' to '/MyFiles/4.jpg' in C:\xampp\htdocs\MyFiles\upload_image.php on line 31
Please upload a proper type.

Now I am kinda confused. I have a file that I have uploaded, and if I remove $_FILES['upload']['name'] it said it uploaded, even though no file is there. I have tried for hours straight to get this but I don't see any difference between my example and the book's.

3
Contributors
2
Replies
17
Views
1 Year
Discussion Span
Last Post by diafol
0

You need to provide the full file system path. So intead of "/MyFiles/4.jpg" use "C:/xampp/htdocs/MyFiles/4.jpg". I suggest you try:

if(move_uploaded_file($_FILES['upload']['tmp_name'] , $_SERVER['DOCUMENT_ROOT'] . "/MyFiles/{$_FILES['upload']['name']
}"))
0

Carrying on from johnny1 - PHP file functions can access locations above the public root, so prefixing locations with '/' means that you are probably referring to - if using Windows - C:\
So, PHP is looking for C:\MyFiles, which probably doesn't exist. If you are saving to a location above the public root then you can use relative prefixes like ../../secretfiles/MyFiles etc - you can also use relative locations within the public directories - you do not have to use $_SERVER['DOCUMENT_ROOT'].

It appears that your file uploader script: upload_image.php is the "view" and the handler all in one. Not usually the way to do it, but anyway, as this file is within the a public directory - I'm assuming public root, you should be able to store it just by the following:

if(move_uploaded_file($_FILES['upload']['tmp_name'] , "MyFiles/{$_FILES['upload']['name']}"))...

Again assuming that the MyFiles directory is in the same parent directory as the upload_image.php file.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.