SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2013
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    PHP Code:

       $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($filenamestrrpos($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.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,032
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    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.

  3. #3
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,998
    Mentioned
    100 Post(s)
    Tagged
    0 Thread(s)
    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?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2013
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanx Figured it out.

    Quote Originally Posted by SpacePhoenix View Post
    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.

  5. #5
    SitePoint Member
    Join Date
    Jan 2014
    Location
    bangalore
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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"];
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •