...

View Full Version : Desperately need help to fix this upload script



ungovernable
05-18-2008, 06:02 AM
Ok well my problem is really weird, i'll try to explain it the best i can

Basically i have this image upload script ive been working a lot on, and with the help of some peoples... so i really dont want to start it over

My problem is that when an image is upload, i won't be able to touch the physical file.. I cannot download it from the ftp to my computer, i cannot change the chmod, but i can rename the file

BUT, i tryed it on a different server and everything works perfectly

Daniel0 suggested the problem was caused by this:

It's because your user nor your group hasn't got write access to the file. It's probably owned by whatever user and group your web server is running as. Only root is allowed to change the ownership using chown so you'll have to give write permissions to all.


BUT... The script is creating a thumbnail file of the uploaded file, and i have no problem with the thumb file!! I can transfer it, change the chmod, etc...

here is what some user of phpfreaks suggested:

Apache may save your upload as one user, then when you modify the original image to create a thumb, php may save as another user. So if you edit without resizing, your permissions will match.


So basically this is where i'm stuck at.... could anyone PLEASE help me to fix this script? I would be really happy if i can get this to work



<html dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
<head>
$headinclude
<title>$vboptions[bbtitle]</title>
</head>
<body>





<?php
include("db.php");
$bbuserinfo['userid'] = "1";

//This is the resize/sampler.
function resample($img, $max_w, $max_h, $type, $thumb)
{

switch ($type) {

case 2:

// get original images width and height
list($or_w, $or_h, $or_t) = getimagesize($img);


// obtain the image's ratio
$ratio = ($or_h / $or_w);

// original image
$or_image = imagecreatefromjpeg($img);

// resize image?
if ($or_w > $max_w || $or_h > $max_h) {

// resize by height, then width (height dominant)
if ($max_h < $max_w)
{

if ($or_h < $or_w)
{
$rs_w = $max_w;
$rs_h = $rs_w * $ratio;
}

else
{
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}


}

// resize by width, then height (width dominant)
if ($max_w < $max_h)
{

if ($or_h < $or_w)
{
$rs_w = $max_w;
$rs_h = $rs_w * $ratio;
}

else
{
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}


}
//Resize by neither (as it is equal anyways)
if ($max_w == $max_h)
{

if ($or_h < $or_w)
{
$rs_w = $max_w;
$rs_h = $rs_w * $ratio;
}

else
{
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}

}


// copy old image to new image
$rs_image = @imagecreatetruecolor($rs_w, $rs_h);
@imagecopyresampled($rs_image, $or_image, 0, 0, 0, 0, $rs_w, $rs_h, $or_w, $or_h);
}
// image requires no resizing
else {
$rs_w = $or_w;
$rs_h = $or_h;

$rs_image = $or_image;
}


if($thumb == true)
{
$image = explode('/',$img);
$x = 0;
$num_dir = count($image)-1;
while($x < count($image)-1)
{
$pre_img .=$image[$x]."/";
$x++;
}

$img = $pre_img."/sml_".$image[count($image)-1];
$img;
}
// generate resized image

imagejpeg($rs_image, $img, 100);


return true;
break;





case 3:


// get original images width and height
list($or_w, $or_h, $or_t) = getimagesize($img);


// obtain the image's ratio
$ratio = ($or_h / $or_w);

// original image
$or_image = imagecreatefrompng($img);

// resize image?
if ($or_w > $max_w || $or_h > $max_h) {

// resize by height, then width (height dominant)
if ($max_h < $max_w)
{

if ($or_h < $or_w)
{
$rs_w = $max_w;
$rs_h = $rs_w * $ratio;
}

else
{
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}


}

// resize by width, then height (width dominant)
if ($max_w < $max_h)
{

if ($or_h < $or_w)
{
$rs_w = $max_w;
$rs_h = $rs_w * $ratio;
}

else
{
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}


}
//Resize by neither (as it is equal anyways)
if ($max_w == $max_h)
{

if ($or_h < $or_w)
{
$rs_w = $max_w;
$rs_h = $rs_w * $ratio;
}

else
{
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}

}


// copy old image to new image
$rs_image = imagecreatetruecolor($rs_w, $rs_h);
imagecopyresampled($rs_image, $or_image, 0, 0, 0, 0, $rs_w, $rs_h, $or_w, $or_h);
}
// image requires no resizing
else {
$rs_w = $or_w;
$rs_h = $or_h;

$rs_image = $or_image;
}

if($thumb == true)
{
$image = explode('/',$img);
$img = $image[(count($image)-1)]."sml_".$image[count($image)];
print $img;
}
// generate resized image
imagepng($rs_image, $img, 100);

return true;
break;






case 4:


// get original images width and height
list($or_w, $or_h, $or_t) = getimagesize($img);


// obtain the image's ratio
$ratio = ($or_h / $or_w);

// original image
$or_image = imagecreatefromgif($img);

// resize image?
if ($or_w > $max_w || $or_h > $max_h) {

// resize by height, then width (height dominant)
if ($max_h < $max_w)
{

if ($or_h < $or_w)
{
$rs_w = $max_w;
$rs_h = $rs_w * $ratio;
}

else
{
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}


}

// resize by width, then height (width dominant)
if ($max_w < $max_h)
{

if ($or_h < $or_w)
{
$rs_w = $max_w;
$rs_h = $rs_w * $ratio;
}

else
{
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}


}
//Resize by neither (as it is equal anyways)
if ($max_w == $max_h)
{

if ($or_h < $or_w)
{
$rs_w = $max_w;
$rs_h = $rs_w * $ratio;
}

else
{
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}

}


// copy old image to new image
$rs_image = imagecreatetruecolor($rs_w, $rs_h);
imagecopyresampled($rs_image, $or_image, 0, 0, 0, 0, $rs_w, $rs_h, $or_w, $or_h);
}
// image requires no resizing
else {
$rs_w = $or_w;
$rs_h = $or_h;

$rs_image = $or_image;
}

// generate resized image
if($thumb == true)
{
$image = explode('/',$img);
$img = $image[(count($image)-1)]."sml_".$image[count($image)];
print $img;
}
imagegif($rs_image, $img, 100);

return true;
break;
}

}

?>








<html dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
<head>
<?php $headinclude;?>
<title><?php $vboptions['bbtitle'];?></title>
</head>
<body>
<br /><br /><br /><br />
<center>

<?php
$bbuserinfo['userid'] = "1";
if($bbuserinfo['userid'] != NULL)
{}
else
{

$url = 'connexion.php'; $timeout = 0;
header('Refresh: ' . $timeout . ';url=' . $url);

print'
<meta http-equiv="refresh" content="'.$timeout.';url='.$url.';">

<script type="text/javascript">
setTimeout(function(){window.location.replace("'.$url.'; ", '.($timeout * 0).');
</script>';

}

if($bbuserinfo['userid'])
{
print'

<form action="'.$_server['php-self'].'" method="post"
enctype="multipart/form-data" id="something" class="uniForm">
';

// @include("imgbands.php"); //

print'
<br /><hr />
<table>
<tr>
<td>
Uploader la photo (#1):
</td>
<td>
<input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />
</td>
</tr>
<tr>
<td>
Description (optionel):
</td>
<td>
<INPUT TYPE="TEXT" NAME="infos" VALUE="" size="60">
</td>
</tr>
</table>




<INPUT TYPE="hidden" NAME="user" VALUE="'.$bbuserinfo['username'].'" size="60">
<input name="submit" type="submit" class="submitButton" value="AJOUTER L\'IMAGE" />
</form>
';

}


$band = $_POST['band'];

if($_POST['submit'])
{
$imagenamz = $_FILES['new_image']['name'];
$originalname = $_FILES['new_image']['name'];
$date = date('dmy');
$imagename = "$band" . "_" . $date . "_" . $originalname;
echo $imagename;
$vraidate = date('d/m/y');
$nom = $_POST['user'];
$infos = $_POST['infos'];


echo "<br />L'image a été ajouté dans l'album photos de <b>$band</b><hr><br />";

$source = $_FILES['new_image']['tmp_name'];
$target = "db/img/images/".$imagename;
move_uploaded_file($source, $target);

$imagepath = $imagename;
$save = "db/img/images/" . $imagepath; //This is the new fileyou saving
$file = "db/img/images/" . $imagepath; //This is the originalfile

list($width, $height, $att) = getimagesize($file) ;


$size = getimagesize($file);
$uploadx = $size[0];
$uploady = $size[1];




}
list($width, $height, $type, $attr) = getimagesize($target);


resample($target, 800, 800, $type, false);
resample($target, 150, 150, $type, true);





if(isset($_POST['submit'])){
// if file exists
// add to database
$filename = 'db/img/images/sml_".$imagepath."';

if (file_exists($filename)) {

$band = $_POST['band'];
$originalname = $_FILES['new_image']['name'];
$date = date('dmy');
$imagename = "$band" . "_" . $date . "_" . $originalname;
// echo $imagename;
$vraidate = date('d/m/y');
$nom = $_POST['user'];
$infos = $_POST['infos'];
$result = mysql_query("INSERT INTO bands_pics (id,band,url,date,infos,valide,user) VALUES ('NULL', '$band', '$imagename','$vraidate','$infos','0','$nom')");
}
}




?>

123

syosoft
05-18-2008, 08:05 AM
sounds like your problem is in move_uploaded_file.
get the file contents of the $_FILES['whatever]['tmp_name'] and dump in a new file you create by hand in the correct loc.

Inigoesdr
05-18-2008, 08:42 AM
sounds like your problem is in move_uploaded_file.
get the file contents of the $_FILES['whatever]['tmp_name'] and dump in a new file you create by hand in the correct loc.
If the function doesn't work because of permissions, copying it manually won't work either. PHP will be running either as the web server user, or the account holder's user. If the file was uploaded/modified using PHP then it should have access to it, and should be able to move it. That said, it is possible that the admin has locked the /tmp directory, but that would likely cause problems elsewhere. ungovernable, you haven't posted any errors you're getting. These contain crucial information that can help solve your problem, so you need to reply with any and all. If you have a permissions problem it will throw a warning at least. Read the FAQ thread in this forum for some helpful information.

ungovernable
05-18-2008, 02:48 PM
ungovernable, you haven't posted any errors you're getting. These contain crucial information that can help solve your problem, so you need to reply with any and all. If you have a permissions problem it will throw a warning at least.

Errors? There are no errors... like i said the script SEEMS to be working. The file will be correctly uploaded to the ftp, and the thumbnail will be made.... my problem is only when i try to download the physical file off my ftp (which is crucial for backups..)

I get no php errors on any of my pages

On the FTP, here is the errors i am getting:


PASV
227 Entering Passive Mode (208,110,80,27,46,153)
RETR Akuma_230408_4489569.jpg
550 Can't open Akuma_230408_4489569.jpg: Permission denied
Can't open Akuma_230408_4489569.jpg: Permission denied
Failed [Can't open Akuma_230408_4489569.jpg: Permission denied]

ungovernable
05-18-2008, 02:50 PM
and like i said there is no problem downloading/modifying thumbnail file

PASV
227 Entering Passive Mode (208,110,80,27,227,136)
RETR sml_Akuma_230408_4489569.jpg
150-Accepted data connection
150 14.0 kbytes to download
226-File successfully transferred
226 0.000 seconds (measured here), 58.00 Mbytes per second
Transferred 14 292 bytes in 2,70 seconds (5,16 KB/s)
Queue completed; transferred 14 292 bytes in 3,47 seconds (4,02 KB/s)

Inigoesdr
05-18-2008, 07:20 PM
chmod() (http://php.net/chmod) the file too 755 or 777 with PHP once it's uploaded. Or chown() (http://php.net/chown) it to the ftp user.

ungovernable
05-19-2008, 03:03 AM
could you give me an example plz? I'm really a newbie and ive been stuck at this point for a while with this script

i have tryed to change the chmod's on my ftp, but i get a similar error

Inigoesdr
05-19-2008, 05:23 PM
Click the links in my post. Each function has examples, and comments in the manual.

ungovernable
05-19-2008, 09:23 PM
well, i just tryed adding


chmod("/$file", 0777);

to the end of my script, and it isn't working

uploaded file will have "chmod 600"

Inigoesdr
05-19-2008, 10:06 PM
No, I wouldn't imagine that would work. The path points to the root directory of the server("/"), and your file isn't likely to be there. You need to use the path of the file you want to get with FTP.

ungovernable
05-20-2008, 12:35 AM
well, if you check the code i posted

$file value is db/img/images/filename.jpg

this is a full path, isn't it?

Inigoesdr
05-20-2008, 01:01 AM
well, if you check the code i posted

$file value is db/img/images/filename.jpg

this is a full path, isn't it?




chmod("/$file", 0777);
^See the slash? That is prepended to whatever you set $file to. On top of that, I'm not going to presume you're using the same code each time you reply to a post. You could have made minor changes that can cause other problems.

ungovernable
05-20-2008, 01:12 AM
well you told me to check this page: http://ca3.php.net/chmod

this is what i did, and here is the example you told me to look at :


<?php
chmod("/somedir/somefile", 755); // notation d&#233;cimale : probablement faux
chmod("/somedir/somefile", "u+rwx,go+rx"); // cha&#238;ne : incorrect
chmod("/somedir/somefile", 0755); // notation octale : valeur du mode correcte
?>


there is a slash at the begginning, right?

However, i have removed this slash from my code, and i still have the same problem with the same errors

Inigoesdr
05-20-2008, 04:25 AM
there is a slash at the begginning, right?
Yes, and that path would be correct if your directory was in the root, was called "somedir", and your file was called "somefile".

However, i have removed this slash from my code, and i still have the same problem with the same errors
Are the permissions being changed? Look at them in the FTP client to see what they are. Have you set the error reporting to a level that would throw errors and/or are you getting any errors?

syosoft
05-20-2008, 11:38 AM
It may be useful to note who you have server space through as well as server details. Some people on the forum may be familiar with that service and be able to provide further insight as to the limitations.

You can always call your hosting provider and ask if this "should" be happening.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum