May 26, 2010

Simple PHP upload form not working

Question by Lea

I seem to run into so many issues when dealing with writing to directories. If someone could please look over this script, and tell me why it’s not working, I’d be so appreciative.

Upon uploading the file from the form, I don’t get anything.. It doesnt output any errors, it simply just refreshes.

Thanks, Lea

<?php include ('config.php');
 if( isset($_POST['submit']) ) {

$target = ''.$_SERVER['DOCUMENT_ROOT'].'/images/';
$target = $target . basename( $_FILES['photo']['name']) ;
$url = basename( $_FILES['photo']['name']) ;

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
$moved =  "File has been moved to location $target";

$name = mysql_real_escape_string($_POST['photoname']);
mysql_query("INSERT INTO photos (photo_name, photo_image) VALUES ('$name',  '$url' )") or die(mysql_error());
$success = "Photo has been added!";

  } else {

$moved = "File has not been moved to $target";
$success = "Failed to upload:(";

  }

} ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Photo Upload</title>
<meta name="robots" content="index, follow" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="<?php echo $globalurl; ?>styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="holder">
<br /><br />
<b>Add a new photo</b>
<hr />
<br />
<b><?php echo "$success<br />"; ?>
<?php echo "$moved<br />"; ?></b>
  <form enctype="multipart/form-data" method="post" action="<?php echo $PHP_SELF; ?>">
    <table cellspacing="0" cellpadding="0" width="500px">
      <tbody>
        <tr>
          <td valign="top">Photo Name:</td>
          <td valign="top">
            <input type="text" name="photoname" /><br />
            <br />
            </td>
        </tr>
        <tr>
          <td valign="top">Photo:</td>
          <td valign="top">
  <input type="file" name="photo"><br />
            <br />
            </td>
        </tr>
      </tbody>
    </table>
    <input type="submit" value="submit" />
  </form>
</div>
</body>
</html>

Answer by Victor Stanciu

Without checking the PHP code for errors, the first thing that stands out is that this:

isset($_POST['submit'])

always returns false. The input type=”submit” must have the name attribute “submit” in order to be sent:

<input type="submit" value="submit" name="submit" />

Answer by Starx

Ok done

<?php 
if( isset($_POST['submit']) ) {
 $target = 'images/';
 echo $target = $target . basename( $_FILES['photo']['name']) ;

 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
  $moved =  "File has been moved to location $target"; 
  $success = "Photo has been added!";
 } 
 else { 
 $moved = "File has not been moved to $target";
 $success = "Failed to upload:(";
  }
} 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Photo Upload</title>
<meta name="robots" content="index, follow" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="<?php echo $globalurl; ?>styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="holder">
<b>Add a new photo</b>
<?php echo "$success<br />"; ?>
<?php echo "$moved<br />"; ?>
  <form enctype="multipart/form-data" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
    <table cellspacing="0" cellpadding="0" width="500px">
      <tbody>
        <tr>
          <td valign="top">Photo Name:</td>
          <td valign="top">
            <input type="text" name="photoname" /><br />
            <br />
            </td>
        </tr>
        <tr>
          <td valign="top">Photo:</td>
          <td valign="top">
    <input type="file" name="photo">
            </td>
        </tr>
      </tbody>
    </table>
    <input type="submit" value="submit" id="submit" name="submit" />
  </form>
</div>
</body>
</html>

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!