<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.

Dani AI

Generated

The error is about the destination path, not the upload itself. On Windows, a leading slash points to the drive root, so /MyFiles/4.jpg means C:\MyFiles\4.jpg, not C:\xampp\htdocs\MyFiles\4.jpg. That is why PHP cannot open the target. and @diafol are right: build a real filesystem path. Using __DIR__ keeps it anchored to the script folder and avoids surprises.

<?php
$destDir = __DIR__ . '/MyFiles';
if (!is_dir($destDir)) {
    mkdir($destDir, 0755, true);
}

$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime  = $finfo->file($_FILES['upload']['tmp_name']);
$allowed = ['image/jpeg' => 'jpg', 'image/png' => 'png'];
if (!isset($allowed[$mime])) {
    exit('Invalid image type.');
}

$base = preg_replace('/[^A-Za-z0-9._-]/', '_', basename($_FILES['upload']['name']));
$target = $destDir . '/' . $base;

if (!move_uploaded_file($_FILES['upload']['tmp_name'], $target)) {
    exit('Could not move uploaded file.'); // do not blame the type here
}
echo 'Uploaded to ' . $target;

Quick checks to tighten things up:

  • Use MAX_FILE_SIZE (not MAX_SIZE) and place that hidden field before the file input. PHP only enforces it if named exactly and positioned correctly. See Handling file uploads.
  • Ensure upload_max_filesize and post_max_size in php.ini are large enough and consistent; the POST cap must be >= the upload cap. See upload_max_filesize and post_max_size.
  • When debugging, log the exact failure and $_FILES array, and confirm the directory exists and is writable.
  • For the move itself, reference: move_uploaded_file. For safe type checking, use Fileinfo: finfo_file.

Recommended Answers

All 2 Replies

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']
}"))
Member Avatar for Member #120589

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.