View Full Version : Having error in upload-procedure

08-05-2007, 07:45 PM
Hi all, since the forums of the Dutch coding-community (at which I'm an active member) generally "aren't that good".. I'll try to get help elsewhere. Great to see that more of these well-populated forums exist.

Anyway, let's get to business.

I'm creating a script, which should upload a file while an URL has been given to the file. And actually everything goes ok, until I go copying the file to my server. And as it isn't a superscript (probably, I think), it's more or less a project to learn and understand a lot about file-handling in PHP.

What's happening?
Getting an error, its output is as followed:
Warning: copy(./avatars/ava1pl.jpg) [function.copy]: failed to open stream: Permission denied in /home/poison/domains/poisonlog.net/public_html/cp_check.php on line 100

And at first, I thought it might have been the permissions. So I turned the CHMOD of my map to maximum (and totally unnecessarily) to 0777. But that didn't work..
So I went checking if Safemode was on, but... Nope, it wasn't.

I've been working at this script as of yesterday, and actually it's getting really boring and frustating that I can't get further than 3 lines each hour, and most of them are empty lines...

My code:

if(isset($_POST['avatar']) && !empty($_POST['avatar']) && $_POST['avatar'] != $result['avatar']){
$allowed_filetypes = "jpg|jpeg|jfif|jpe|gif|png";
if(!preg_match('/^(http:\/\/)?([\w\-\.]+)\:?([0-9]*)\/([^ \?&=\#\"\n\r\t<]*?(\.('.$allowed_filetypes.')))$/',$_POST['avatar'],$farr)){
$input_error['Avatar URL'] = "De avatar voldoet niet aan de eisen (alleen ".str_replace("|",", ",$allowed_filetypes)." toegestaan).";
$bget = '/' . $farr[4];
$port = (!empty($farr[3])) ? $farr[3] : 80;
if(!($fp = @fsockopen($farr[2], $port, $errno, $errstr))){
$input_error['Avatar openen'] = "De avatar kon niet geopend worden. Controleer de opgegeven URL (".$_POST['avatar'].").";
fputs($fp, "GET $bget HTTP/1.1\r\n");
fputs($fp, "HOST: " . $farr[2] . "\r\n");
fputs($fp, "Connection: close\r\n\r\n");


$fdata = '';
$fdata .= fread($fp,1024*50);

if(!preg_match('#Content-Length\: ([0-9]+)[^ /][\s]+#i', $fdata, $fsizearr) || !preg_match('#Content-Type\: image/[x\-]*([a-z]+)[\s]+#i', $fdata, $ftypearr)){
$input_error['Avatar toegang'] = "Het programma had problemen met het uitlezen van het avatar-bestand. Mogelijk wordt dit veroorzaakt door een kapot bestand.";
$max_avatar_size = (1024*$avatar_max_sizekb);
$filesize = $fsizearr[1];
$filetype = $ftypearr[1];

if($filesize > $max_avatar_size){
$input_error['Avatar grootte'] = "Je avatar mag niet groter zijn dan ".($max_avatar_size/1024)." KB. Uw avatar is ".round($filesize/1024)." KB.";

$fdata = substr($fdata,(strlen($fdata)-$filesize),$filesize);
$extension = $farr[6];

$path = './avatars/';
$tmpfname = tempnam(realpath($path),'avapl');

$tmpfopen = fopen($tmpfname,"w");
$tmpfwrite = fwrite($tmpfopen,$fdata,$filesize);


list($width,$height,$type) = getimagesize($tmpfname);
echo "Bestandsgrootte: ".$filesize." Bytes<br />";
echo "Bestandstype: ".$filetype."<br />";
echo "Bestandsbreedte: ".$width."<br />";
echo "Bestandshoogte: ".$height."<br />";

if($tmpfwrite < $filesize){
$input_error['Bestand schrijven'] = "Het bestand kon niet worden geschreven.";
if($width>0 && $height>0 && $width<=$avatar_max_width && $height<=$avatar_max_height){
$imagename = "ava".$_SESSION['user_id']."pl.".$extension;
$query = "UPDATE users SET avatar='' WHERE user_id='".$_SESSION['user_id']."'";
mysql_query($query) or $error .= Error("00035","Kan avatar-gegevens niet updaten.");
$avatar = "";
$avatar = $result['avatar'];

I don't think you will be distracted too much by the few Dutch sentences as errors or output, since I use English variables.
If you need any clarification to a variable or text or anything, please ask. I'm getting totally desperate with this code.

08-06-2007, 01:16 AM
"So I turned the CHMOD of my map to maximum (and totally unnecessarily) to 0777. But that didn't work.."

./avatars/ is the folder that requires write perms. What are you referring to by "map" in the above statement? Just change ./avatars to 777.

08-06-2007, 11:12 AM
Oh my, it works now.. Very, very many thanks:thumbsup:

(I really thought I had it on 777 before)