Jump to content

upload script won't always work


garydt

Recommended Posts

Some users can upload photos while others can't. What would cause this?

Upload form

 <form name="form1" id="Upload" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data" method="POST"> 
             <p> 
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>"> 
        </p> 
        <h3> 
          <input name="file" type="file" id="file" onchange="MM_showHideLayers('Layer1','','hide')" />
          <span class="style1">for your photo</span> </h3>
        <p>
          <label>
          <input name="sent" type="hidden" id="sent" value="1" />
          </label>
        </p>
	<h3 class="style7">
          <input type="text" name="textfield" />
          <span class="style2 style1">Add caption for the photo</span>          </h3>
            <input name="submit" type="submit" id="submit" onclick="MM_showHideLayers('Layer1','','hide','Layer3','','show')" value="Upload your photo"> 
        </p> 
     
        <input type="hidden" name="MM_insert" value="form1">
</form>

 

Script to upload, resize to 2 sizes and rename file.

$editFormAction = '';
$max_file_size = 1024*1024*1024; //1MB
if(isset($_POST['sent'])){
unset($submit);
if (isset($_POST['sent'])) $submit = true;
else die('Error: No file got sent');
if($_FILES['file']['name'])
   {
      $arr  = getimagesize($_FILES['file']['tmp_name']); 

      list($width, $height, $type, $attr) = getimagesize($_FILES['file']['tmp_name']); 
       if($type == FALSE)
      {
         header("Location: uploadform2.php");
         exit();
      }
               elseif($type != 2)
                {

      header("Location: uploadform2.php");
           exit();
      }   
   }

$fieldname = $_FILES['file'];
function resizeimage($name){
$filename = './uploads/'.$name;
	if(file_exists($filename)) {
		$image		= imagecreatefromjpeg($filename); //unresized image
		$im_info	= getimagesize($filename); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 100px; max width: 100px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 140;
		}else {
			$im_divice	= $im_height / 140;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "thumbnails/".$name, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
}

function resizelarger($name){
$filename = './uploads/'.$name;
	if(file_exists($filename)) {
		$image		= imagecreatefromjpeg($filename); //unresized image
		$im_info	= getimagesize($filename); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 100px; max width: 100px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 300;
		}else {
			$im_divice	= $im_height / 300;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "smallpics/".$name, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
       }

// make a note of the directory that will recieve the uploaded file 
// full url $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/'; 
$uploadsDirectory = './uploads/'; 

//Upload image
$uploadFilename = $uploadsDirectory.$fieldname['name'];

// now let's move the file to its final location and allocate the new filename to it 
move_uploaded_file($fieldname['tmp_name'], $uploadFilename);

$user = ($_SESSION['MM_Username']);
$a = rand(0,9999);
resizeimage($fieldname['name']);
rename("thumbnails/".$fieldname['name'], "thumbnails/".$user.$a.$fieldname['name']);
$smallimage = './thumbnails/'.$user.$a.$fieldname['name'];

resizelarger($fieldname['name']);
rename("smallpics/".$fieldname['name'], "smallpics/".$user.$a.$fieldname['name']);
$smallpics = './smallpics/'.$user.$a.$fieldname['name'];

unlink($uploadFilename);

Why does it work for some users and not for others?

Link to comment
Share on other sites

there are a couple things that need to be done when changing the file upload size.  all of them should be done through the php.ini settings file.

 

file_uploads 1                    # file uploads on
upload_max_filesize 5M       #here, enter the number followed by K (kilobyte), M (megabyte), G (gigabyte)
post_max_size 5M             # equal to or greater than max filesize
max_execution_time 60        # in seconds, needs to be high enough to prevent timeouts, can also use set_time_limit(0) at top of php script
max_input_time 60              #time values need to be set high enough for files to upload

 

also, if you declare settings from your php script, use the ini_set() function

 

gl :)

Link to comment
Share on other sites

Hi there garydt,

 

I have struggled with this exact same problem for ages. Unfortunately, no one could answer the 'white page' issue definitively. The server's php.ini file checks many variables that the image has to comply with before it will be transferred (uploaded) to it's final destination.

 

I don't know why the white page occurs without displaying an error, but my theory is that the server runs out of memory. When you are uploading a JPEG file (or any other image format for that matter) the image is already compressed on your hard drive. The server must first uncompress the image to process the data contained within. To get an accurate measurment of how much memory is required, multiply the H x W of the image by 4 (4 bytes of data per pixel). The result will give you a relative size of the image uncompressed.

 

Eg: 1200 x 800 image (1200 x 800 x 4= 3840000) - 3.84 MB

 

Try setting the php_ini limit higher at the top of your script using ini_set. Try 64MB.

Also, the server may have a limit of the actual pixel size that it will allow, for example, 1500 x 1500.

Try uploading a small image, but with a large file size (eg. 800 x 600 at 3MB) then try uploading a large image with a small file size (eg. 2500 x 2500 at 250Kb) and see what you get.

 

Obviously, there are tons more suggestions I could give you, but this is a process of elimination to eradicate what is not wrong. Give that a try and let us know how it goes. I hope it helps you out!

 

Regards,

Iceman

Link to comment
Share on other sites

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.