Page 1 of 1

Upload not working...

Posted: Thu May 22, 2008 3:14 pm
by jasonhardwick
So I've got this code that takes an image that is uploaded resizes it and renames it... but it doesnt work properly i know i'm missing something just dont know what...

Code: Select all

 
<?php
include "config.php";
 
$user_image = $_FILES['user_image']['tmp_name'];
$filename1 =  $_FILES['user_image']['name'];
 
function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 
 
$src1 = imagecreatefromjpeg($user_image);
 
list($width1,$height1)=getimagesize($user_image);
 
$newwidth1=250;
$newheight1=($height1/$width1)*250;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
 
imagecopyresampled($tmp1,$src1,0,0,0,0,$newwidth1,$newheight1,$width1,$height1);
 
$tbl_name="user";
$username=$_SESSION['username'];
 
$ext = findexts ($_FILES[$filename1]['name']) ;
 
$new_name = $username."."; 
imagejpeg($tmp1,$target,100);
$target = "user_images/";
$target1 = $target . $new_name.$ext;
 
if(move_uploaded_file($_FILES[$filename1]['tmp_name'], $target1)) 
{
$sql=( "UPDATE $tbl_name SET user_image = '".$new_name.$ext."' WHERE uname='$username'");
$rows = mysql_query($sql);
echo "Your profile image has been sucessfully updated";
$id = mysql_insert_id();
echo $id;
 
} 
else
{
echo "Sorry, there was a problem uploading your file.";
}
 
imagedestroy($src1);
imagedestroy($tmp1); 
?>
 

Re: Upload not working...

Posted: Thu May 22, 2008 3:41 pm
by VladSun
What's not working?

Re: Upload not working...

Posted: Thu May 22, 2008 3:54 pm
by jasonhardwick
Well all I get is an output of this:

Code: Select all

???m?-Sorry, there was a problem uploading your file.

Re: Upload not working...

Posted: Thu May 22, 2008 3:56 pm
by VladSun
Set permission to 0777 of "user_images" directory.

Re: Upload not working...

Posted: Thu May 22, 2008 4:06 pm
by jasonhardwick
Nope... that didnt work for me still the same problem

Re: Upload not working...

Posted: Thu May 22, 2008 6:26 pm
by RobertGonzalez
Change:
$_FILES[$filename1]['tmp_name']

to:
$_FILES['user_image']['tmp_name']

And see if that does anything.

Re: Upload not working...

Posted: Thu May 22, 2008 7:19 pm
by whiterabbit
jasonhardwick wrote:Well all I get is an output of this:

Code: Select all

???m?-Sorry, there was a problem uploading your file.
One thing that I find very useful when writing and debugging code, is to have error messages that are more verbose than what you would have as a finished product and display to your end users. It is very hard to know why your file failed to upload if all it says is 'There was a problem.' What I would do, is put this after your "Sorry, there was a problem uploading your file"..

<pre>
print_r($_FILES);
</pre>

This should tell you any relevant error messages that might have occurred. Specifically the $_FILES['user_image']['error']. If you get an error code, you can look it up here http://us2.php.net/manual/en/features.f ... errors.php as it should tell you exactly what went wrong.

Also, just as an FYI, there is a much easier way to get the extension of your file: pathinfo($filename). See http://us2.php.net/manual/en/function.pathinfo.php

Re: Upload not working...

Posted: Thu May 22, 2008 7:28 pm
by jasonhardwick
yeah that makes the image upload with the new name but dosent resize the file... thats why i ran it with the $FILENAME1 to try and run the resize first then pass that to the rename part of the code...