Uploading File ...
Warning: move_uploaded_file(/var/www/html/php-books/php_and-mysql-web-development/chapter17/uploads/php.png): Failed to open stream: No such file or directory in /var/www/html/php-books/php-and-mysql-web-development/chapter17/writing-the-php-to-deal-with-the-file-pag-382/upload.php on line 49
Warning: move_uploaded_file(): Unable to move "/tmp/phprGaLtZ" to "/var/www/html/php-books/php_and-mysql-web-development/chapter17/uploads/php.png" in /var/www/html/php-books/php-and-mysql-web-development/chapter17/writing-the-php-to-deal-with-the-file-pag-382/upload.php on line 49
Problem: Could not move file to destination directory.
upload.hml
<!DOCTYPE html>
<html>
<head>
<title>Upload a File</title>
</head>
<body>
<h1>Upload a File</h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
<label for="the_file">Upload a file:</label>
<input type="file" name="the_file" id="the_file"/>
<input type="submit" value="Upload File"/>
</form>
</body>
</html>
upload.php
<!DOCTYPE html>
<html>
<head>
<title>Uploading... </title>
</head>
<body>
<h1>Uploading File ...</h1>
<?php
if($_FILES['the_file']['error'] > 0)
{
echo 'Problem: ';
switch($_FILES['the_file']['error'])
{
case 1:
echo 'File exceeded upload_max_filesize.';
break;
case 2:
echo 'File exceeded max_file_size.';
break;
case 3:
echo 'File only partially uploaded.';
break;
case 4:
echo 'No file uploaded.';
break;
case 6:
echo 'Cannot uploaded file: No temp directory specified';
break;
case 7:
echo 'Upload failed: Cannot write to disk.';
break;
case 8:
echo 'A PHP extension blocked the file upload.';
break;
}
exit;
}
//Does the file have the right MIME type ?
if($_FILES['the_file']['type'] != 'image/png')
{
echo 'Problem: file is not a PNG image.';
exit;
}
//put the file where we'd like it
$upload_file = '/var/www/html/php-books/php_and-mysql-web-development/chapter17/uploads/'.$_FILES['the_file']['name'];
if(is_uploaded_file($_FILES['the_file']['tmp_name']))
{
if(!move_uploaded_file($_FILES['the_file']['tmp_name'],$upload_file))
{
echo 'Problem: Could not move file to destination directory.';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename';
echo $_FILES['the_file']['name'];
exit;
}
echo 'File uploaded successfully.';
//show what was uploaded
echo '<p>You uploaded the following image:<br/></p>';
echo '<img src="/uploads/'.$_FILES['the_file']['name'] .'"/>';
?>
</body>
</html>