0

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 image</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?

5
  • 1
    "Nothing happens" indicates that you aren't displaying error messages or haven't even tried to debug ... get some (error)output first. Commented Mar 18, 2011 at 15:13
  • @ChrisR - yup, that's what I'm thinking... Commented Mar 18, 2011 at 15:14
  • no i did that and i fix what error i had, but this time i get nada. Commented Mar 18, 2011 at 15:16
  • We are talking about logic errors, not syntax errors. No language will catch a logic error. Commented Mar 18, 2011 at 15:18
  • i check the code again and it like its not executing this part of the code 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 image</a>"); echo "Upload complete!"; } } it only echo out select a file Commented Mar 18, 2011 at 15:28

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

2
  1. Format you're code.
  2. 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!)
  3. Learn to debug - actually put conditions elsewhere and see where your code is failing!

7 Comments

Agree. Let's see your HTML, I'm thinking the form is not multipart either.
<html> upload file...<br> <form action='up.php' method='POST' enctype='multipart/form'> <input type='file' name='myfile'><p> <input type='submit' name='submit' value="upload"> </html>
@user621799 - FORMAT YOUR CODE!
@user621799 - That's also not Valid HTML. You never close your form and you don't even have a submit button! Post your full HTML form, FORMATTED
@user621799: Is this a joke? :-)
|
1

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);

3 Comments

here is the form, i do have that line of code already <html> upload file...<br> <form action='up.php' method='POST' enctype='multipart/form'> <input type='file' name='myfile'><p> <input type='submit' name='submit' value="upload"> </html>
nada, i check the code again and it like its not executing this part of the code 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 image</a>"); echo "Upload complete!"; } } it only echo out select a file
Why are you checking for the $name if you are checking for an error? Shouldn't you use $error?
-1

Just in case you can't see any errors activate error reporting by setting error_reporting(E_ALL); right after the php tag.

1 Comment

nada, i check the code again and it like its not executing this part of the code 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 image</a>"); echo "Upload complete!"; } } it only echo select a file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.