Jump to content

Recommended Posts

not sure if this has ever happen with anyone and not sure if its a problem with php or whatever, but basically what is happening is with a couple images I am testing with will not upload. Other images will upload fine, when I ran into this problem I even tried with files with the same aspect ratio and close to same file size and they work fine.

 

I have tried uploading the same image that fails with two completely different scripts and still fails. tried changing the name of the pic, made a copy of the pic, opened it up in my photo editing software and re-save the image( kept it same image size but it ended up making the file size larger ) and all failed.

 

my first image upload script showed me warnings:

 

 

Warning: getimagesize(files/apic.jpg): failed to open stream: No such file or directory in ...\picupload.php on line 62

Warning: Division by zero in ...\picupload.php on line 64

Warning: imagecreatetruecolor(): Invalid image dimensions in ...\picupload.php on line 73

Warning: imagecreatefromjpeg(files/apic.jpg): failed to open stream: No such file or directory in ...\picupload.php on line 74

Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in ...\picupload.php on line75

Warning: imagejpeg() expects parameter 1 to be resource, boolean given in ...\picupload.php on line 76

the Directory "DOES" exist, due to other photos have been uploaded.

 

here is the script to the one that shows me the warning messages:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image Upload</title>
<script src="js/editor.js" type="text/javascript"></script>
</head>

<body>
<?php
$query = 'SELECT * FROM news';
if ($stmt = mysqli_prepare($con, $query)) {

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* store result */
    mysqli_stmt_store_result($stmt);
}
switch($_POST['uploaded'])
{
	case 'true': 
if(isset($_FILES['files'])){
    $errors= array();
	foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
		$file_name = strtolower($_FILES['files']['name'][$key]);
		$file_size =$_FILES['files']['size'][$key];
		$file_tmp =$_FILES['files']['tmp_name'][$key];
		$file_type=$_FILES['files']['type'][$key];	
        if($file_size > 6291456){
			$errors[]='File size must be less than 4 MB';
        }		
        $desired_dir="files";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);		// Create directory if it does not exist
				mkdir("$desired_dir/thumb", 0700);
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"files/".$file_name);
				$width = 268;
				$height = 200;
				list($width_orig, $height_orig) = getimagesize("files/".$file_name);

				$ratio_orig = $width_orig/$height_orig;
				
				if ($width/$height > $ratio_orig) {
				   $width = $height*$ratio_orig;
				} else {
				   $height = $width/$ratio_orig;
				}
				
				// Resample
				$image_p = imagecreatetruecolor($width, $height);
				$image = imagecreatefromjpeg("files/".$file_name);
				imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
				imagejpeg($image_p, "files/thumb/".$file_name, 100);
				$filename = $file_name;
            }else{									//rename the file if another one exist
                $new_dir=$file_name.time();
                 rename($file_tmp,"files/".$new_dir);
				 $filename = $new_dir;
            }
			?>
            Upload Finished: <?php echo $filename ?>
            <button onClick="findwrap('<?php echo $filename; ?>'); return false;">Close</button>
            <?php
        }else{
                print_r($errors);
        }
    }
}
break;
default:
?>
- Select a photo to upload.<br>
- Wait for it to finish upload and click the "Close" button<br>
- Not the "x" or it won't work!!
<form  action="" method="POST" enctype="multipart/form-data">
	<input type="file" name="files[]" /><br />
    <input hidden="true" name="uploaded" type="hidden" value="true">
	<input type="submit" />
</form>
<?php
}
?>
</body>
</html>

the other script i used was one that I found online:

https://app.box.com/shared/bbkbdsmz7b

 

they both work with other images, just not this one. I just don't want to have this happen with my client.

 

here is the pic I am trying to upload as a test pic( to large to upload to form ), and yes its a picture of my cat... lol :

https://www.dropbox.com/s/i5s2hbi21mqodnn/iPhone%20Pictures%20009.JPG

Edited by Guber-X

your code doesn't have error checking logic in it to determine that the upload worked, before using any of the uploaded file information, nor is it testing that it successfully moved the uploaded file to the destination before trying to use the file at the destination.

 

the code you find or write must test at each step that can fail due to an error of any kind, if the expected data actually exists before trying to use that data.

 

code that doesn't have error checking logic in it, is only about half finished. it may work as expected when everything is prefect, but when anything is wrong, it doesn't tell you that it failed or where or why it failed.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.