SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast paramedic's Avatar
    Join Date
    Oct 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    uploading images

    Hi guys.

    I have set up a web page where you can upload up to 5 images to my database.( name to database and image to the server). I use the following code to choose an image
    Code:
    <label>Photograph 1: </label><input type="file" name="photo_1" /><br />
    <label>Photograph 2: </label><input type="file" name="photo_2" /><br />
    <label>Photograph 3: </label><input type="file" name="photo_3" /><br />
    <label>Photograph 4: </label><input type="file" name="photo_4" /><br />
    <label>Photograph 5: </label><input type="file" name="photo_5" /><br />
    and the following code to upload the imgae.
    Code:
    $photo_1=$_FILES['photo_1']['name'];
    $photo_2=$_FILES['photo_2']['name'];
    $photo_3=$_FILES['photo_3']['name'];
    $photo_4=$_FILES['photo_4']['name'];
    $photo_5=$_FILES['photo_5']['name'];
    
    
    
    $files = array('photo_1', 'photo_2', 'photo_3', 'photo_4', 'photo_5'); 
    foreach($files as $file){     
    $uploadedfile = $_FILES[$file]['tmp_name'];     
    $src = imagecreatefromjpeg($uploadedfile);     
    list($width,$height)=getimagesize($uploadedfile);     
    $newwidth=135;     
    $newheight=($height/$width)*135;     
    $tmp=imagecreatetruecolor($newwidth,$newheight);     
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);     
    $filename = 'images/'. $_FILES[$file]['name'];     
    imagejpeg($tmp,$filename,100);     
    imagedestroy($src);     
    imagedestroy($tmp); }
    I then insert in the database in the normal way.This all works fine,however

    The problem is

    If I dont upload all 5 images I get an error as the code is expecting an image. I have no idea how to sort this out. ANY HELP would be appreciated.

  2. #2
    SitePoint Zealot smadeira's Avatar
    Join Date
    Oct 2003
    Location
    Pennsylvania
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you posted the error message that would be helpful. My guess is that if you only upload 3 images then there are no values for $_FILES['photo_4'] and $_FILES['photo_5']. You need to check the length of $_FILES[] to see how many values were returned to the server and then adjust your loop that processes the files accordingly.

    Read the $_FILES into $files like this:

    PHP Code:
    for ($i 1$i count($_FILES)+1$i++){
        
    $files[$i-1] = $_FILES["photo_$i"];

    Get rid of this line:

    PHP Code:
    $files = array('photo_1''photo_2''photo_3''photo_4''photo_5'); 
    the you should be able to process $files as you show in your code.
    Scott


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
  •