PHP Image Upload Problem

Discussion in 'PHP' started by tronik, Apr 7, 2012.

  1. #1
    I'm working on a add movie script for my free movies site, everything works. The image upload works for the movie poster, but there is a 2nd image upload in the script which is meant to be optional(for featured movie), and whenever I add a image to it, it doesn't work. Everything gets added to the database just fine, but it won't upload the featured movie image(image folders permissions are 777). I don't know why it's doing this obviously, it's basically the same code as the movie poster upload, it also doesn't track the width and height for the featured image. Please help!

    
    <?php
    
    
    if(isset($_POST['submit_movie'])){
    
    
    $movie_name = clean($_POST['movie_name']);
    $movie_description = clean($_POST['description']);
    $image_type = $_FILES['movie_poster']['type'];
    $feat_type = $_FILES['movie_feat']['type'];
    
    
    if(!empty($_FILES['movie_poster']['tmp_name'])){
    list($iwidth, $iheight, $itype, $iattr) = getimagesize($_FILES['movie_poster']['tmp_name']);
    }elseif(!empty($_FILES['movie_feat']['tmp_name'])){
    list($fwidth, $fheight, $ftype, $fattr) = getimagesize($_FILES['movie_feat']['tmp_name']);
    }
    
    
    if(!$movie_name || !$_FILES['movie_poster']['tmp_name']){
        echo error("You didn't fill out all the required fields.");
        }elseif(strlen($movie_name) < 2 || strlen($movie_name) > 75){
        echo error("Movie name must be between 2 and 75 characters.");
        }elseif(strlen($movie_description) > 2000){
        echo error("Movie description cannot exceed 2000 characters.");
        }elseif($image_type != "image/jpeg" && $image_type != "image/jpg" && $image_type != "image/png" && $image_type != "image/gif"){
        echo error("Movie poster image type must have one of these extensions: jpeg, jpg, png, or gif");
        }elseif(!empty($_FILES['movie_feat']['tmp_name']) && $feat_type != "image/jpeg" && $feat_type != "image/jpg" && $feat_type != "image/png" && $feat_type != "image/gif"){
        echo error("Movie featured image type must have one of these extensions: jpeg, jpg, png, or gif");
        }elseif($iwidth != 160 && $iheight != 237){
        echo error("Movie poster image must be 160x237 pixels.");
        }elseif(!empty($_FILES['movie_feat']['tmp_name']) && $fwidth != 214 && $fheight != 337){
        echo error("Movie featured image must be 214x337 pixels.");
        }else{
    
    
        if($image_type == "image/jpeg"){ $image_type = ".jpeg"; }
        if($image_type == "image/jpg"){ $image_type = ".jpg"; }
        if($image_type == "image/png"){ $image_type = ".png"; }
        if($image_type == "image/gif"){ $image_type = ".gif"; }
        
        if($feat_type == "image/jpeg"){ $feat_type = ".jpeg"; }
        if($feat_type == "image/jpg"){ $feat_type = ".jpg"; }
        if($feat_type == "image/png"){ $feat_type = ".png"; }
        if($feat_type == "image/gif"){ $feat_type = ".gif"; }
        
        $movie_image = strtolower(str_replace(" ", "-", $movie_name));
        $movie_image1 = "{$movie_image}{$image_type}";
        $movie_image2 = "{$movie_image}{$feat_type}";
        $target_path = "../images/movies/";
        $target_path2 = "../images/movies/featured/";
        
        $target_path = $target_path . basename($movie_image1); 
        $target_path2 = $target_path2 . basename($movie_image2); 
        $date_added = time();
        $release_date = $_POST['month'] . " " . $_POST['day'] . ", " . $_POST['year'];
        $release_date = clean($release_date);
    
    
            if(move_uploaded_file($_FILES['movie_poster']['tmp_name'], $target_path) || move_uploaded_file($_FILES['movie_feat']['tmp_name'], $target_path2)){
            $movie_image1 = clean($movie_image1);
            $movie_image2 = clean($movie_image2);
            mysql_query("INSERT INTO movies(movie_name, description, poster_image, feat_image, date_added, release_date)
            VALUES('{$movie_name}', '{$movie_description}', '{$movie_image1}', '{$movie_image2}', '{$date_added}', '{$release_date}')") or die(mysql_error());
            echo success("Movie has successfully been added. <a href=\"?page=movies\">Go Back</a><br /><br />");
            include("afoot.php");
            die;
            }else{
            echo error("Error uploding movie images. Pleas try again.");
            }
    }
        
    }
    
    
    ?>
    <form method="post" enctype="multipart/form-data">
    <table cellspacing="4" cellpadding="4 border="0" width="90%">
    <tr>
    <td><strong>Movie Name:</strong></td><td><input type="text" name="movie_name" value="<? echo $movie_name; ?>"/></td>
    </tr>
    <tr>
    <td><strong>Movie Description:</strong></td>
    <td><textarea name="description" rows="6" cols="70"><? echo $movie_description; ?></textarea></td>
    </tr>
    <tr>
    <td><strong>Release Date:</strong></td>
    <td>
    <select name="month">
    <option>January</option>
    <option>February</option>
    <option>March</option>
    <option>April</option>
    <option>May</option>
    <option>June</option>
    <option>July</option>
    <option>August</option>
    <option>September</option>
    <option>October</option>
    <option>November</option>
    <option>December</option>
    </select>
    <select name="day">
    <option>01</option>
    <option>02</option>
    <option>03</option>
    <option>04</option>
    <option>05</option>
    <option>06</option>
    <option>07</option>
    <option>08</option>
    <option>09</option>
    <?php
    for($i = 10; $i <= 31; $i++){
    echo "<option>$i</option>";
    }
    ?>
    </select>
    <select name="year">
    <?php
    $year = date("Y");
    for($i = $year; $i >= 1900; $i--){
    echo "<option>$i</option>";
    }
    ?>
    </select>
    </td>
    </tr>
    <tr>
    <td><strong>Movie Poster Image:</strong></td><td><input type="file" name="movie_poster" /> <i><span style="color: red">Must be 160x237 pixels.</span></i></td>
    </tr>
    <tr>
    <td><strong>Movie Featured Image:</strong></td><td><input type="file" name="movie_feat" /> <span style="color: red"><i>Must be 214x337 pixels.</span></i><br />(optional)</td>
    </tr>
    <tr><td colspan="2"><hr></td></tr>
    <tr>
    <td></td><td><input type="submit" name="submit_movie" value="Submit Movie" /></td>
    </tr>
    </table>
    </form>
    
    Code (markup):

     
    tronik, Apr 7, 2012 IP
  2. xintern

    xintern Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Make sure the featured folder is CHMOD 777 if it is try 755.
     
    xintern, Apr 7, 2012 IP
  3. tronik

    tronik Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Already tried doesn't work.
     
    tronik, Apr 7, 2012 IP
  4. kaunomanas

    kaunomanas Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    if(move_uploaded_file($_FILES['movie_poster']['tmp_name'], $target_path) || move_uploaded_file($_FILES['movie_feat']['tmp_name'], $target_path2)){
    Code (markup):
    i think problem in this line, because if first file is uploaded other is not. or in other words: if first function returns true other will not be executed, and program will run further inside if.
     
    Last edited: Apr 7, 2012
    kaunomanas, Apr 7, 2012 IP
  5. tronik

    tronik Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That was the problem, but this error still doesn't work. I can ignore this feature for now, but I need it to check the width and height of the images in the future.
    
    }elseif(!empty($_FILES['movie_feat']['tmp_name']) && $fwidth != 214 && $fheight != 337){
    	echo error("Movie featured image must be 214x337 pixels.");
    
    Code (markup):
     
    tronik, Apr 8, 2012 IP