Jump to content

Recommended Posts

Hey guys i have been using a working upload and resize function for some time now. It's used on a lot of my websites but for some reason i can't make it work in this projet.

 

The files is realy uploaded

Array
(
    [image] => Array
        (
            [name] => 4186-Porte_int_rieur24__1600x1200_.jpg
            [type] => image/pjpeg
            [tmp_name] => /tmp/phpl9dEYT
            [error] => 0
            [size] => 95748
        )

)

 

But for some reason /tmp/phpl9dEYT seems to be returning an error

 

Warning: getimagesize(/tmp/phpl9dEYT) [function.getimagesize]: failed to open stream: No such file or directory in /var/www/vhosts/cameleonmedia.com/subdomains/patrick/httpdocs/mtlmode/administration/function.php on line 280

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /var/www/vhosts/cameleonmedia.com/subdomains/patrick/httpdocs/mtlmode/administration/function.php on line 298

Warning: imagecreatefromjpeg(/tmp/phpl9dEYT) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /var/www/vhosts/cameleonmedia.com/subdomains/patrick/httpdocs/mtlmode/administration/function.php on line 299

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /var/www/vhosts/cameleonmedia.com/subdomains/patrick/httpdocs/mtlmode/administration/function.php on line 300

Warning: imagejpeg(): supplied argument is not a valid Image resource in /var/www/vhosts/cameleonmedia.com/subdomains/patrick/httpdocs/mtlmode/administration/function.php on line 301

 

This is my code 2 function. 1 is to upload and 1 is to rename

 

function nomValide($tmp){
$accents = "ÀÃÂÃÄÅà áâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÃÃŽÃìíîïÙÚÛÜùúûüÿÑñ";
$pasaccents = "aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn";
$autorises = ".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";

$tmp = strtr($tmp,$accents,$pasaccents);

for($i=0;$i<strlen($tmp);$i++){
	if(!ereg($tmp{$i},$autorises)){
		$tmp = str_replace($tmp{$i},'_',$tmp);
	}
}
return $tmp;
}

function photo($image,$largeur_maximum,$destination){
$autorises = 'gifjpegjpgpngGIFJPEGJPGPNG';
$extension = explode('.',$destination);
$extension = strtolower($extension[sizeof($extension)-1]);

if(ereg($extension,$autorises)){
	$dimensions = getimagesize($image);
	$largeur_actuelle = $dimensions[0];
	$hauteur_actuelle = $dimensions[1];

	if ($largeur_maximum=="original"){
	$nouvelle_largeur = $dimensions[0];
	$nouvelle_hauteur = $dimensions[1];	
	}else{
	if($largeur_actuelle > $largeur_maximum){
	$nouvelle_largeur = $largeur_maximum;
	$nouvelle_hauteur = $hauteur_actuelle / ($largeur_actuelle / $largeur_maximum);
	} else {
	$nouvelle_largeur = $dimensions[0];
	$nouvelle_hauteur = $dimensions[1];
	}
	}
	   
	if($extension == 'jpg' || $extension == 'jpeg'){
	$image_p = imagecreatetruecolor($nouvelle_largeur, $nouvelle_hauteur);
	$image = imagecreatefromjpeg($image);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $nouvelle_largeur, $nouvelle_hauteur, $largeur_actuelle, $hauteur_actuelle);
	imagejpeg($image_p,$image);
	} else if($extension == 'gif'){
	$image_p = imagecreatetruecolor($nouvelle_largeur, $nouvelle_hauteur);
	$image = imagecreatefromgif($image);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $nouvelle_largeur, $nouvelle_hauteur, $largeur_actuelle, $hauteur_actuelle);
	imagegif($image_p,$image);
	} else if($extension == 'png'){	   
	$image_p = imagecreatetruecolor($nouvelle_largeur, $nouvelle_hauteur);
	$image = imagecreatefrompng($image);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $nouvelle_largeur, $nouvelle_hauteur, $largeur_actuelle, $hauteur_actuelle);
	imagepng($image_p,$image);
	}
	copy($image,$destination);
}
}

 

And when i wana use it i simply do this:

 

if ($_FILES['image']['size'] != "0") {
$rand = rand("1000", "90000") . "-";
photo($_FILES['image']['tmp_name'],150,'../media/small/'.$rand.nomValide($_FILES['image']['name']));
}

 

Like i said it has been working great on other websites.

Before you ask, YES ../media/small/ exist and is chomoded to 777.

 

So wa da ya think?

Link to comment
https://forums.phpfreaks.com/topic/178674-solved-upload-problem/
Share on other sites

Kinda strange!

 

try creating a tmp folder in the media folder (with write access)

then move the uploaded file to it,

ie

$temp = dirname(__FILE__).'/../media/tmp/'.$rand.nomValide($_FILES['image']['name']);
$img = dirname(__FILE__).'/../media/small/'.$rand.nomValide($_FILES['image']['name']);

if(move_uploaded_file ( $_FILES['image']['tmp_name'], $temp))
{
photo($temp,150,$img);
unlink($temp);
}

 

So the file failed to move ?

I don't think you got the same error's

I guess i should of added an else

 

if(move_uploaded_file ( $_FILES['image']['tmp_name'], $temp))
{
photo($temp,150,$img);
unlink($temp);
}else{
echo "Error moving file";
}

 

I think upload_tmp_dir is only changeable via the php.ini file itself

 

you could try it i guess

ini_set('upload_tmp_dir','/new/path');

Well as your not getting an error 1,6 or 7, it seams the upload is fine, but accessing the file isn't working..

 

you could try this little script to see if you can access files,

$tmpfname = tempnam("/tmp", "FOO");
if(is_readable($tmpfname ){
$handle = fopen($tmpfname, "w");
fwrite($handle, "writing to tempfile");
fclose($handle);
unlink($tmpfname);
}

 

but personally i would seam to be a problem with permission, unless i am missing something

No it's not a permission prob

 

httpdocs/mtlmode/administration/function.php

That's where the function is

 

httpdocs/mtlmode/administration/pub.php

That's where the function is used

 

httpdocs/mtlmode/media/small/

That's where the file is uploaded to

 

Both "media" and "small" are set to 777 and the groups are good as well

 

The string to acess the download directory used from mtlmode/administration/pub.php is ../media/small/

 

I tryed your code and fixed the missing ) of if(is_readable($tmpfname ){

But it returned nothing.

OMG ... this is sooooo stupid !!! When i updated my script with the new image upload, i forgot to take out the old one ...

 

                                    // Move the file

                                    if (move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/' . $rand . $_FILES['image']['name'])) {

                                        $image = $rand . $_FILES['image']['name'];

                                    } else {

                                        $message = $message = "- There was an error moving the file.<br>";

                                    }

 

When the file was moved the tmp was deleted by PHP ... So the seconde script trying to upload did not work ... Grrr ... No idea why i forgot that ... sorry abbout that MadTechie. I guess we would of found it faster if i would of provided you the full code.

Ahhh that make more sense :)

LOL, at least its working now :)

 

Erm just one thing

$message = $message = "- There was an error moving the file.<br>";

 

if your appending the text then surely this would be what you want :P

$message .= "- There was an error moving the file.<br>";

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.