I was trying to write a PHP Upload script, I'm using a basic script from a book as the starting point, and I was trying to make a more complicated version except I'm completely confused by an early error.
My complete code is below. This script does not work, I simply get the HTML form and "No image has been uploaded" as the output.
Everything to do with MySQL has been commented out, everything to do with the file validation has been commented out. It's a little messy, but I was trying to get the basic upload script working before adding more complicated features - learning as I go.
Although the script above doesn't work, this one DOES:
This script works fine - as far as I can see the scripts should be almost identical (excluding the commented out code). The first script did briefly work, but as I uncommented code it stopped working again. The problem is, I've now commented out everything that's different but it's still not working. :huh:
I'm using XAMPP to test, running PHP Version 5.4.7. Any suggestions?
My complete code is below. This script does not work, I simply get the HTML form and "No image has been uploaded" as the output.
Code:
<?php // upload.php
//####################### Get DB Info #######################
/*require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: ". mysql_error());
//######################## Functions ########################
function mysql_fix_string($string)
{
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return mysql_real_escape_string($string);
}
function mysql_entities_fix_string($string)
{
return htmlentities(mysql_fix_string($string));
}*/
//######################## HTML Form ########################
echo <<<_END
<html><head><title>PHP Form Upload</title></head><body>
<form method='post' action='upload.php' enctype=multipart/form-data'>
Select File: <input type='file' name='filename' size='10'/>
<input type='submit' value='Upload' />
</form>
_END;
//###########################################################
if ($_FILES)
{
echo "why does this not work?";
/*
$name = $_FILES['filename']['name'];
switch($_FILES['filename']['type'])
{
case 'image/jpeg': $ext = 'jpg' ; break;
case 'image/gif': $ext = 'gif'; break;
case 'image/png': $ext = 'png'; break;
default: $ext = ''; break;
}
if ($_FILES)
{
//begin mysql
$timestamp = time();
$size = $_FILES['filename']['size'];
$user = 'unregistered';
*/
//begin upload
$name = $_FILES['filename']['name'];
move_uploaded_file($_FILES['filename']['tmp_name'], $name);
echo "Uploaded image '$name' <br /><img src='$name' />";
//}
//else echo "'$name' is not an accepted image file";
}
else echo "No image has been uploaded";
echo "</body></html>";
//###########################################################
?>
Everything to do with MySQL has been commented out, everything to do with the file validation has been commented out. It's a little messy, but I was trying to get the basic upload script working before adding more complicated features - learning as I go.
Although the script above doesn't work, this one DOES:
Code:
<?php //upload_test_only.php
echo <<<_END
<html><head><title>PHP Form Upload</title></head><body>
<form method='post' action='upload_test_only.php' enctype='multipart/form-data'>
Select File: <input type='file' name='filename' size='10' />
<input type='submit' value='Upload' />
</form>
_END;
if ($_FILES)
{
$name = $_FILES['filename']['name'];
move_uploaded_file($_FILES['filename']['tmp_name'], $name);
echo "Uploaded image '$name' <br /><img src='$name' />";
}
echo "</body></html>";
?>
This script works fine - as far as I can see the scripts should be almost identical (excluding the commented out code). The first script did briefly work, but as I uncommented code it stopped working again. The problem is, I've now commented out everything that's different but it's still not working. :huh:
I'm using XAMPP to test, running PHP Version 5.4.7. Any suggestions?