Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: PHP Upload - Not Working, No Error.

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default PHP Upload - Not Working, No Error.

    Hi,

    I have 6 upload fields to add images to a "project."

    The HTML for the form is as follows:
    HTML Code:
    	<form method="post" action="cms_artwork.php?artwork_page=add_proj" enctype="multipart/form-data">
    		<table cellpadding="3px" cellspacing="3px">
    			<tr>
    				<td>
    					<strong>one:</strong> <input type="file" name="img1" /><br />
    					<em>caption:</em> <input type="text" name="cap1" />
    				</td>
    				<td>
    					<strong>two:</strong> <input type="file" name="img2" /><br />
    					<em>caption:</em> <input type="text" name="cap2" />
    				</td>
    			</tr>
    			<tr>
    				<td>
    					<strong>three:</strong> <input type="file" name="img3" /><br />
    					<em>caption:</em> <input type="text" name="cap3" />
    				</td>
    				<td>
    					<strong>four:</strong> <input type="file" name="img4" /><br />
    					<em>caption:</em> <input type="text" name="cap4" />
    				</td>
    			</tr>
    			<tr>
    				<td>
    					<strong>five:</strong> <input type="file" name="img5" /><br />
    					<em>caption:</em> <input type="text" name="cap5" />
    				</td>
    				<td>
    					<strong>six:</strong> <input type="file" name="img6" /><br />
    					<em>caption:</em> <input type="text" name="cap6" />
    				</td>
    			</tr>
    			<tr>
    				<td colspan="2">
    					<input type="hidden" name="projid" value="<?php echo $projid; ?>" />
    					<input type="submit" value="Add Images" id="config_submit" />
    				</td>
    			</tr>
    		</table>
    	</form>
    The PHP is as follows:
    PHP Code:
    $projid $_POST['projid'];
    $projTitle 'Undefined Title';
    $result mysql_query("SELECT * FROM artwork_projs WHERE projid='$projid'");
    $count mysql_num_rows($result);
    if(
    $count 0){
        
    $row1 mysql_fetch_assoc($result);
        
    $projTitle $row1['title'];
        
    $result mysql_query("SELECT img_num FROM artwork_images WHERE projid='$projid' ORDER BY img_num ASC");
        
    $count mysql_num_rows($result);
        if(
    $count 0){
            
    $row mysql_fetch_assoc($result);
            
    $lastNum array_pop($row);
            
    $firstNum $lastNum 1;
            echo 
    $firstNum;
        }
        else{
            
    $firstNum 1;
        }
        
    $upload_success false;
        
    print_r($_FILES);
        function 
    addImages($inputName){
            global 
    $firstNum$projid$upload_success;
            if((!empty(
    $_FILES[$inputName])) && ($_FILES[$inputName]['error'] == 0)) {
                
    $filename basename($_FILES[$inputName]['name']);
                
    $ext substr($filenamestrrpos($filename'.') + 1);
                if ((
    $ext == 'jpg') || ($_FILES[$inputName]['type'] == 'image/jpeg')){
                    
    $newname 'imgs/artwork/'.$projid.'_'.$firstNum.'.'.$ext;
                    if(
    file_exists($newname)){
                        
    unlink($newname);
                    }
                    if(
    move_uploaded_file($_FILES[$inputName]['tmp_name'],$newname)){
                        
    $capInputNum substr($inputName,3);
                        
    $imgCaption = isset($_POST['cap'.$capInputNum]) ? trim($_POST['cap'.$capInputNum]) : '<span></span>';
                        
    $result mysql_query("INSERT INTO artwork_images (projid,img_num,main,caption) VALUES ('$projid','$firstNum','no','$imgCaption'");
                        
    $firstNum++;
                        
    $upload_success true;
                    }
                    else{
                        echo 
    '<p>'.$inputName.' NOT UPLOADED</p>'."\n";
                    }
                }
                else{
                    echo 
    '<p>'.$inputName.' NOT A JPG</p>'."\n";
                }
            }
        }
        for(
    $i=1;$i<=6;$i++){
            
    addImages('img'.$i);
        }
        if(
    $upload_success == true){
            echo 
    '<h2>Images Successfully Added to <em>'.$projTitle.'</em></h2>'."\n";
            echo 
    '<p>The images you added to <em>'.$projTitle.'</em> are now part of the project.</p>'."\n";
            echo 
    '<p>'.$firstNum.'</p>'."\n";
        }
        else{
            echo 
    '<h2>There was a problem</h2>'."\n";
            echo 
    '<p>For some reason, the images for <em>'.$projTitle.'</em> were not added successfully. Please try again.</p>'."\n";
        }
    }
    else{
        
    redirect2('cms_artwork.php?artwork_page=add_proj&projDoesNotExist');

    When I print_r($_FILES);, I get the following:
    Code:
    Array ( 
    	[img1] => Array ( 
    		[name] => artworktestj2.jpg 
    		[type] => image/jpeg 
    		[tmp_name] => /tmp/phpuFLMRW 
    		[error] => 0 [size] => 3698 ) 
    	[img2] => Array ( 
    		[name] => 
    		[type] => 
    		[tmp_name] => 
    		[error] => 4 
    		[size] => 0 ) 
    	[img3] => Array ( 
    		[name] => [type] => 
    		[tmp_name] => 
    		[error] => 4 
    		[size] => 0 ) 
    	[img4] => Array ( 
    		[name] => 
    		[type] => 
    		[tmp_name] => 
    		[error] => 4 
    		[size] => 0 ) 
    	[img5] => Array ( 
    		[name] => 
    		[type] => 
    		[tmp_name] => 
    		[error] => 4 
    		[size] => 0 ) 
    	[img6] => Array ( 
    		[name] => 
    		[type] => 
    		[tmp_name] => 
    		[error] => 4 
    		[size] => 0 ) )
    Now, I've made sure the folder has the correct permissions, that PHP's temp directory is set to "/tmp", that the max upload size is 5M, etc. I know it might work better if I set the names of the upload inputs to images[] or something, to force the array, but I don't know how to include the individual captions that way (would I do caption[] or something?). If anyone has any suggestions or ideas as to why this isn't working, your help is appreciated. Thank you!
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Compare:
    Code:
    [img1] => Array ( 
    		[name] => artworktestj2.jpg 
    		[type] => image/jpeg 
    		[tmp_name] => /tmp/phpuFLMRW 
    		[error] => 0 
    		[size] => 3698 )
    error => 0 indicates the file was uploaded successfully. can your script access this file?
    Code:
    	[img2] => Array ( 
    		[name] => 
    		[type] => 
    		[tmp_name] => 
    		[error] => 4 
    		[size] => 0 )
    error => 4 means that no file was found. are you sure additional files were actually uploaded?

    If you need more help, please try to describe your problem more fully.

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Not all of the 6 fields may be filled in. In the example, only one of the 6 actually contained a file. If one of the fields doesn't have a file, does that somehow inhibit the images that do exist from being uploaded?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    There's nothing wrong with that. But your code might not continue checking, for example. It doesn't look like your current code is designed to do that though.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Any suggestions, Dan?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I looked and didn't see anything until:
    Your use of $upload_success is not very accurate: you're using a single variable to check multiple values. It's better if you use $upload_success as an array and add the name of each uploaded file to it. Then later check which files were uploaded (or if the array is empty).
    That might be causing your problems.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by alexjewell View Post
    Not all of the 6 fields may be filled in. In the example, only one of the 6 actually contained a file. If one of the fields doesn't have a file, does that somehow inhibit the images that do exist from being uploaded?
    No; I was just trying to get a better understanding of your problem.

    To clarify, in your example, you are not accessing the first uploaded file, correct? where, exactly, are you failing? is your addImages() function ever called? do you get the "there was a problem" message?

    Daniel's right about your $upload_success variable, but I think it would only cause confusion in the sense that the script wouldn't be able to tell if one upload failed, but others succeeded. for example:
    case 1) upload 3 files; they all fail: script says "There was a problem."
    case 2) upload 3 files; they all succeed: script says "Images successfully added."
    case 3) upload 3 files; one succeeds and two fail: script says "Images successfully added." It's true (technically), it ignores the failed uploads because nothing ever resets the $upload_success if there is a problem.
    Last edited by traq; 02-16-2011 at 07:50 PM.

  8. #8
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    addImages is called in the for statement immediately after the function:

    PHP Code:
    for($i=1;$i<=6;$i++){
        
    addImages('img'.$i);

    There are 6 fields, so it calls the function 6 times for each one.

    The idea of using an array for $upload_success makes sense, but it doesn't make sense that it wouldn't upload the file itself. What boggles my mind, though, is that $upload_success is changed, meaning PHP thinks move_uploaded_file worked when it in fact has not. Again, no error is thrown, and I have E_ALL error reporting turned on. Furthermore, the $result mysql_query() does not work, either; the image information is not added to the database, either.

    I tried uploading 6 at once, and it still didn't work. I got back the following info:

    Code:
    Array ( 
    	[img1] => Array ( 
    		[name] => artworktestj.jpg 
    		[type] => image/jpeg 
    		[tmp_name] => /tmp/phpcu0nBb 
    		[error] => 0 [size] => 3698 ) 
    	[img2] => Array ( 
    		[name] => artworktestj2.jpg 
    		[type] => image/jpeg 
    		[tmp_name] => /tmp/phppQANBk 
    		[error] => 0 
    		[size] => 3698 ) 
    	[img3] => Array ( 
    		[name] => artworktestj.jpg 
    		[type] => image/jpeg 
    		[tmp_name] => /tmp/phpA0okCt 
    		[error] => 0 [size] => 3698 ) 
    	[img4] => Array ( 
    		[name] => artworktestj2.jpg 
    		[type] => image/jpeg 
    		[tmp_name] => /tmp/phpsKE8YC 
    		[error] => 0 
    		[size] => 3698 ) 
    	[img5] => Array ( 
    		[name] => artworktestj.jpg 
    		[type] => image/jpeg 
    		[tmp_name] => /tmp/phpo60AmM 
    		[error] => 0 [size] => 3698 ) 
    	[img6] => Array ( 
    		[name] => artworktestj2.jpg 
    		[type] => image/jpeg 
    		[tmp_name] => /tmp/phpZqt9JV 
    		[error] => 0 
    		[size] => 3698 ) )
    So, one of the problems is that I actually do not know when the script is failing: PHP is acting like everything is fine and dandy, but neither the database nor imgs/artwork/ directory is showing that it is.

    If there are no obvious problems, what better way do you suggest going about this?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  9. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I can see that line. I was questioning if your script was actually getting to that point or not. Since you say $upload_success is changed to TRUE, I'll assume that it is.

    If you don't mind, I'll copy your code here and see if I can figure out where things are going wrong. I'll let you know.

  10. #10
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    I've resolved the issue! It's always something small. In this case, it was a MySQL error. I forgot to close the parentheses for the VALUES(). Now the script is working like a charm. Thanks so much for your help!
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

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
  •