I have the following PHP code that I am using to upload an image to MySQL. When I click submit nothing happens.
include ("connect.php");
session_start();
$login = $_SESSION['wname'];
if((@$_POST['submit'])&&(isset($_FILES["myfile"]))){
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];
if($name){
die("Error uploading file! Code $error.");
} else {
$place = "avatars/$name";
move_uploaded_file($tmp_name,$place);
$query = mysql_query("UPDATE page SET pid_image_name = '$place' WHERE wname = '$login' ");
die("upload complete <a href='index.php'>View imag开发者_Python百科e</a>");
echo "Upload complete!";
}
} else {
die("select a file");
}
Here's my form:
<form action='up.php' method='POST' enctype='multipart/form'>
<input type='file' name='myfile'>
<p> <input type='submit' name='submit' value="upload">
What am I doing wrong?
Another important parameter to check out in php.ini is post_max_size. If you set upload_max_filesize > post_max_size the result is that no data is written to $_POST nor $_FILES for files which sizes exceed post_max_size but are below upload_max_filesize
- Format you're code.
- Are you submitting the actual HTML form (not the PHP code) as html/multipart?? I am willing to be you're not. (
enctype="multipart/form-data" needs added to your form tag!
) - Learn to debug - actually put conditions elsewhere and see where your code is failing!
In your HTML for have you got enctype="multipart/form-data"
?
Something like this:
<form action='submit.php' method='post' enctype="multipart/form-data">
Edit 1:
if you do this:
if(move_uploaded_file($tmp_name, $place)){
echo "did move file<br />";
}else{
echo "move failed<br />";
}
You will get move failed (Or I do with your code)
Edit 2 I found your problem:
you misspelt the temp-dir variable: you defined
$temp
but in the move_uploaded_file
you asked for $tmp_name
so here the correct code:move_uploaded_file($temp, $place);
Just in case you can't see any errors activate error reporting by setting error_reporting(E_ALL);
right after the php tag.
精彩评论