SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Aug 2004
    Location
    UK
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with file upload script

    Hello peeps,

    I'm stuck with a problem with my file uploading script. It displays the form OK but then I try and upload a file and it gives me the following error msg:

    No file was submitted!

    I've had a good look at it and I'm stumped. I'm also a bit stressed coz I have to catch a train to catch my flight to Mexico in 5 1/2hrs so any of your help would be greatly appreciated!

    Thanks,

    Sion

    PHP Code:
    <?php
    error_reporting
    (E_ALL & ~E_NOTICE);

    // connect to the DB server
      
    include("dbcnx.php");

        
    // if file has been uploaded, process it!
        
    if (($_FILES['PhotoUpload'])) {

        
    $contentfolder "/home/aristadev/mainwebsite_html/randompics/";
        
    $temp_name $_FILES['Upload']['tmp_name'];
        
    $file_name $_FILES['Upload']['name'];
        
    $file_type $_FILES['Upload']['type'];
     

        
    // check that a valid file name was submitted
        
    if ($file_name == "") {
            echo(
    "No file was submitted!<br><br>");
        } else {

        
    // check to see if file is an image
    if (!(($file_type == "image/pjpeg") or ($file_type == "image/jpeg"))) {
            echo(
    "You many only upload JPEG pictures!<br><br><a href='picupload.php'>Try Again</a>");
        } else {
        
        
        
    $file_path $contentfolder.$file_name;
        
    $URL "/randompics/$file_name";
        
        
        
    $move_photo  =  move_uploaded_file($temp_name$file_path);
        
          if (!(
    file_exists($contentfolder.$file_name))) {
          
          if (
    $move_photo) {
        
    $sql_insert_url "INSERT INTO photos SET
                           URL='
    $URL'";

        if (@
    mysql_query($sql_insert_url)) {
            echo(
    "Photo uploaded successfully!<br><br><a href='upload.php'>Upload another photo</a> | <a href='index.php'> Return to CV list</a>");
        } else {
            echo(
    "Error during file upload!" mysql_error() . "<br><br><a href='upload.php'>Try Again</a> | <a href='index.php'> Return to CV list</a>");
        }
        } else {
       echo 
    "Oops! The file already exists. Please rename the picture and upload it again";
      
    }
        
    }
    }
    }
    }

     
      if ((
    $_GET['delete'])) {
        
    // deletes CV from DB
        
    $photoid $_GET['delete'];
        



        
    $sql_check_photo = @mysql_query("SELECT URL FROM photos WHERE ID='$photoid'");
        if (!(
    $sql_check_photo)) {
        echo(
    "Error finding database entry for picture $photoidmysql_error() . "<br><br>");
        } else {
        
    $row mysql_fetch_row($sql_check_photo);
        
    $photourl $row[0];   
           
    $photo "/home/aristadev/mainwebsite_html$photourl";

                if (!(
    unlink($photo))) {
                echo(
    "Error deleting picture");
                } else {
                
    $sql_delete_photo "DELETE FROM photos WHERE ID='$photoid'";

                    if (@
    mysql_query($sql_delete_photo)) {
                    echo(
    "The photo has been deleted from the database!<br><br>");
                    } else {
                    echo(
    "Error deleting photo: " mysql_error());
                    }
                }
                }
        
      }
        
    // show form
        
        // sql to fetch photoinfo info from the DB
        
    $sql_fetch_photos = @mysql_query("SELECT ID, URL FROM photos");
        if (!
    $sql_fetch_photos) {
          echo(
    "Error fetching photo info from the database: " mysql_error());
          exit();
        }
    ?>
        Click browse to find the picture and then hit 'Upload Pic' button!<br><form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data"><input type="file" name="PhotoUpload"><br><input type="submit" name="uploadpic" value="UPLOAD PIC"><br><br></form>
            
            
          
            <?php
            
            
    //display pictures
              
    while ($result mysql_fetch_array($sql_fetch_photos)) {
              
    $photoid $result['ID'];
              
    $photourl $result['URL'];
            
              echo(
    "<a href='picupload.php?delete=$photoid'>Delete</a><br><img src='$photourl'><br><br>");
            }
                
    ?>

  2. #2
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's because you are looking in the variable $file_name which is currently set to $_FILES['Upload']['name'];

    You should check the $temp_name. That is, if $temp_name=="" then no file was submitted.

    Here is some sample code for what I use:
    PHP Code:
    <? 
    if (in_array($_FILES['userfile']['type'], array('image/gif''image/jpeg''image/pjpeg'))) { 
        if (
    $_FILES['userfile']['tmp_name'] == '' || $_FILES['userfile']['tmp_name'] == 'none') { 
        echo 
    'No file uploaded!'
        } else { 
        
    // Move the file to the current directory, with the original filename 
        
    move_uploaded_file($_FILES['userfile']['tmp_name'], "./{$_FILES['userfile']['name']}"); 
        echo 
    'Filename: '$_FILES['userfile']['name']; 
        } 
    } else { 
        
    // Fail, file uploaded is not an image 

    ?>
    The field name for the image is "userfile" in the above example

  3. #3
    SitePoint Member
    Join Date
    Aug 2004
    Location
    UK
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much!


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
  •