File upload script

I have been trying to figure this out for days and have made 0 progress. I have a WORKING script that is a form that posts to a database and includes an upload field. It works fine and as long as you upload a file it puts the info for that upload field in the db record. What I need it to do is if there is no photo uploaded then put images/picholder.jpg in that field so it puts images/picholder.jpg in the db record.



   $details   = isset($_POST['Details'])   ? $_POST['Details']   : '';
   $status  = isset($_POST['Status'])  ? $_POST['Status']  : '';
   $photo=($_FILES['photo']['name']);

 if((!empty($_FILES["photo"])) && ($_FILES['photo']['error'] == 0)) {

  //Check if the file is a valid image and it's size is less than 350Kb
  $filename = basename($_FILES['photo']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "JPEG" || $ext == "pjpeg" || $ext == "gif" || $ext == "png")
 &&  ($_FILES["photo"]["size"] < 350000)) {

    //Determine the path to which we want to save this file /images/ can be changed to where ever you want base to be
        $newname = dirname(__FILE__).'/images/'.$folder.'/'.$filename;

      //Check if the file with the same name already exists on the server
      if (!file_exists($newname)) {

        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['photo']['tmp_name'],$newname))) {

           echo "<center><h3>It's done! The file  ".$filename;
           echo "<center><h4>has been saved in the new folder ".$folder;
           echo "<center><h4>and the listing was added to the Database!";
           echo "<center><h4><a href='listview.php'>View Listing</a>";
        } else {
         echo "<center><h3>Error: A problem occurred during file upload!";
        echo "<center><h4><a href='listadd.php'>Go Back</a>";
        }
      } else {
         echo "<center><h3>Error: File ".$_FILES["photo"]["name"]." already exists!";
         echo "<center><h4><a href='listadd.php'>Go Back</a>";
      }
  } else {
     echo "<center><h3>Error: Only .jpg .gif and .png images under 350Kb are accepted for upload!";
     echo "<center><h4><a href='listadd.php'>Go Back</a>";
  }
}
else {

echo "<center><h3>No file uploaded";

}
?>   

I have tried putting several different things in. But nothing has worked.

Thanks in advance.

Hi myliberty, welcome to the forums,

Sorry, I’m tired ATM. But I don’t see any database related lines in the example code you posted.

When you’re saving a new listing to the database (the code that you’ve posted does not interact with any database), is the image for an item being stored in the same table as the details for a listing?

Thanx. I changed my upload approach and got it working.

Hello myliberty,

You can use this code for file upload

if ($_FILES[“file”][“error”] > 0)
{
echo "Error: " . $_FILES[“file”][“error”] . “<br>”;
}
else
{
echo "Upload: " . $_FILES[“file”][“name”] . “<br>”;
echo "Type: " . $_FILES[“file”][“type”] . “<br>”;
echo “Size: " . ($_FILES[“file”][“size”] / 1024) . " kB<br>”;
echo "Stored in: " . $_FILES[“file”][“tmp_name”];
}