...

View Full Version : Uploading files



harkly
10-22-2012, 07:51 PM
I am trying to upload images and the script is generally working. My problems lies when up loading anything over 7M the script appears to stops running and I get all sort of errors related to an "empty" file.

I have my phpini set to- upload_max_filesize 12M - so shouldn't my file still up load?

Here is my code:


if ($one != NULL) {
$filename = stripslashes($_FILES['photo_1']['name']);

$extension = getExtension($filename);
$extension = strtolower($extension);

$image =$_FILES["photo_1"]["name"];
$uploadedfile = $_FILES['photo_1']['tmp_name'];

if ($image) {
$filename = stripslashes($_FILES['photo_1']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
$errorMsg1="&errorMsg1=1";
}
// if there are not extension errors
else {
// setting it
if($extension=="jpg" || $extension=="jpeg" ) {
$uploadedfile = $_FILES['photo_1']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png") {
$uploadedfile = $_FILES['photo_1']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else if($extension=="gif") {
$uploadedfile = $_FILES['photo_1']['tmp_name'];
$src = imagecreatefromgif($uploadedfile);
}

// getting the width and height of the image
list($width,$height)=getimagesize($uploadedfile);

if ($width > $height) {
// changes width
$newwidth=300;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
} // END if ($width > $height)
else {
// changes height
$newHeight=300;
$newWidth=($width/$height)*$newHeight;
$tmp=imagecreatetruecolor($newWidth,$newHeight);
imagecopyresampled($tmp,$src,0,0,0,0,$newWidth,$newHeight,$width,$height);
}

$extension = strrchr($_FILES['photo_1']['name'],'.');
$extension = strtolower($extension);

// where it places the new images
$photoNumber="_1";
$finalName="$clientID$photoNumber";
$save_path = "uploads/";
$target_path = $save_path . basename( $_FILES['photo_1']['name']);
$NewPhotoName = $finalName;
$withExt = $NewPhotoName . $extension;
$filename = $save_path . $NewPhotoName . $extension;

if(move_uploaded_file($_FILES['photo_1']['tmp_name'], $filename)) {
$stmt = $mysqli->prepare("UPDATE photos SET photo_1=? WHERE userID=?");
$stmt->bind_param('ss',
$withExt,
$clientID);
$stmt->execute();
} // END if(move_uploaded_file($_FILES['photo_1']['tmp_name'], $filename))

// needed for the resizing
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
} // END else
} // END if ($image)

$qry = "SELECT photo_1 FROM photos WHERE userID LIKE '$clientID'";
$result = $mysqli->query($qry);

while ($r=$result->fetch_assoc()) {
$photo_1=$r['photo_1'];
}
} // END else if ($one != NULL)



It works for smaller files and will work for the big ones if I take out everything but


if ($one != NULL) {
// where it places the new images
$photoNumber="_1";
$finalName="$clientID$photoNumber";
$save_path = "uploads/";
$target_path = $save_path . basename( $_FILES['photo_1']['name']);
$NewPhotoName = $finalName;
$withExt = $NewPhotoName . $extension;
$filename = $save_path . $NewPhotoName . $extension;

if(move_uploaded_file($_FILES['photo_1']['tmp_name'], $filename)) {
$stmt = $mysqli->prepare("UPDATE photos SET photo_1=? WHERE userID=?");
$stmt->bind_param('ss',
$withExt,
$clientID);
$stmt->execute();
} // END else if ($one != NULL)

}


Could really use some help. It is most likely a simple issue that I cannot see :mad:

Fou-Lu
10-22-2012, 08:20 PM
Did you increase the post_max_size as well? This directive must be > upload_max_filesize if you want to be able to upload up to the specified upload filesize.

harkly
10-22-2012, 08:38 PM
I didn't but it is set at 33M. I read that it should be doulbe the upload_max_filesize


Also, it takes that image and turns it black and small, 1.27 kb from a 7681kb file

patryk
10-22-2012, 09:30 PM
maybe it's just timing out
check this guide: http://www.radinks.com/upload/config.php

Fou-Lu
10-22-2012, 09:43 PM
I didn't but it is set at 33M. I read that it should be doulbe the upload_max_filesize


Also, it takes that image and turns it black and small, 1.27 kb from a 7681kb file

What's in the associated 'error' offset for the $_FILES? The above looks to me that it's hitting condition 3, or partial file upload.

Time could be of relevance as well, although typically I'd expect that you would lose out on most of the script handling with that. If the script fails, I'd expect that it will never move, unless it fails after the move (which is certainly possible as well). Still, it smells to me that its partial.

BTW, you did restart your webserver software after changing the ini right? Or is it a local ini (if its local, it should work without restart assuming you are on a fcgi install).

harkly
10-22-2012, 09:58 PM
checked the possibily of timing out and I don't believe that is the issue.


What's in the associated 'error' offset for the $_FILES? The above looks to me that it's hitting condition 3, or partial file upload.


You went over my head with this question. Not sure what you mean by assocaisted 'error' offest for the $_FILES.

I am working off of godaddy so not sure if it restarts, I know I had to wait a bit for the 12M to change from 8M

Fou-Lu
10-22-2012, 10:58 PM
What is in $_FILES['photo_1']['error']? You should always be checking this btw, only ever attempt a move if that results to UPLOAD_ERR_OK (or 0).

harkly
10-23-2012, 06:41 PM
The problem appears that the image uploading is not an actual image.

So now I am testing that the uploads are images by checking the getimagesize. However that is not working for me either. Everything is coming back as "not an image"


function getExtension($str) {
$i = strrpos($str,".");
if (!$i) {
return "";
}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

if( isset($_POST['submitPhoto'])) {

$one = getimagesize($_FILES['photo_1']['name']);

if(empty($one)) {// No Image?
echo "The uploaded file doesn't seem to be an image.";
}
else {
echo " an image";
}
}
echo"
<form enctype='multipart/form-data' action='' method='POST'>

&nbsp;<input type='file' name='photo_1' class='zip'><br><br>

<div id='button'><br>
<input type='image' value='Upload File' src='img/button_update.gif' width='72' height='25'
onmouseover=\"javascript:this.src='img/button_update2.gif';\" onmouseout=\"javascript:this.src='img/button_update.gif';\">
<input type='hidden' name='submitPhoto' value='true'>
</div>
</form>";

Error msg:

Warning: getimagesize(ce1.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /home/content/01/6514501/html/singles/testImage.php on line 23
The uploaded file doesn't seem to be an image.

Fou-Lu
10-23-2012, 07:03 PM
You cannot pull a getimagesize on an uploaded file's name. You must pull it from the tmp_name.

harkly
10-23-2012, 07:37 PM
I was attempting to follow this guys example



$file = $_FILES['image_file'];

$file_name = $file['name'];

$error = ''; // Empty

// Get File Extension (if any)

$ext = strtolower(substr(strrchr($file_name, "."), 1));

// Check for a correct extension. The image file hasn't an extension? Add one

if($validation_type == 1)
{

$file_info = getimagesize($_FILES['image_file']['tmp_name']);

{

So this is wrong as well?

Fou-Lu
10-23-2012, 07:42 PM
No, that's correct. It uses the tmp_name: getimagesize($_FILES['image_file']['tmp_name']); you have used the name: getimagesize($_FILES['photo_1']['name']).

harkly
10-23-2012, 09:27 PM
I believe I have it!

I have a question on size.

In the example data they have this line in the form

<input type="hidden" name="MAX_FILE_SIZE" value="768000" />

If the image size if over the limit it gives the same error as using a non image. Is there a way to get a different error message when the file is to large? Not sure how that is deciphered.

I was under the impress that the file had to be uploaded and then tested for size.

Fou-Lu
10-23-2012, 10:12 PM
I guess this depends entirely on what you are checking for your error. If the files uploaded error message is UPLOAD_ERR_FORM_SIZE, then that indicates you've exceeded the max size allowed in the form. Since you do not have a file to work with (or potentially a partial file if obeyed by the client), there is no way to tell what it was. That said, UPLOAD_ERR_FORM_SIZE shouldn't be relied on either as its controlled from a client side. So yes, you still have to test the size after the upload; the MAX_FILE_SIZE is a client control. I don't know the communication between the use of this, but I'd presume that the webhost is instructed to "give up" on it at that point (the client developers may actually know if this is in any way, shape or form actually enforced). PHP wise, it simply issues that its exceeded, but I don't know if it gets the file completely, detects that its exceeded and abandons it, or what it does tbh.

Beyond this, so long as UPLOAD_ERR_OK is valid, then you check to see what you have been given. There's no way until you've received the file to determine exactly what it was.

harkly
10-24-2012, 07:45 PM
Not 100% sure where to place the UPLOAD_ERR_OK. I've place it after the upload, which makes sense to me but then... :o

Can you tell me if I have used it correctly?



move_uploaded_file($_FILES['photo_1']['tmp_name'], $filename);

if($_FILES['photo_1']['error'] == UPLOAD_ERR_OK) {
$stmt = $mysqli->prepare("UPDATE photos SET photo_1=? WHERE userID=?");
$stmt->bind_param('ss',
$withExt,
$clientID);
$stmt->execute();
}
else {
// error for when file didn't move
}

Fou-Lu
10-24-2012, 08:12 PM
No, you do absolutely nothing with the file until its been verified as uploaded successfully. You don't even bother checking the size, the name, the extension, not a single thing until it passes UPLOAD_ERR_OK.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum