Jump to content

Recommended Posts

Hello,

 

i have a php script to upload image files. Once the image is sent, the script will upload it to a folder on my server... So far everything is good. If i install it on my old server (with php4) everything is working fine - i just have to upload the script and create the folder, not even touching the chmod's and letting the default settings. I can also access the image on my FTP and everything is good.

 

My problem is that i use it on a newer server, with php5. I understand nothing of this weird error.. The script is installed the same way than on the old server...

 

I will be able to upload the image, but my problem is when i connect to my FTP, my image is here, but i can't do anything with it.

 

Here is the FTP error i am getting when i try to upload any images with the script:

550 Can't open my_image.jpg: Permission denied

Can't open my_image.jpg: Permission denied

Failed [Can't open my_image.jpg: Permission denied]

 

I can rename the file, but i can't change the rights, and i can't transfer it from my ftp (but i can view the image in my browser)

 

 

Note that the script is uploading the big image, and it is also making a thumbnail file. I have no problem with the thumb, i can change the rights, open it, transfer it on my ftp, etc.... The only problem is around the BIG image....

 

 

 

 

 

 

Here is the code of the PHP script for uploading images:

 

 

<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')");
}
}




?>

Someone help me plz! I'm desperate!

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.

 

See: http://en.wikipedia.org/wiki/File_system_permissions#Traditional_Unix_permissions

but this wouldnt explain why i have no problem with my thumbs

 

the script creates 2 images: the file itself, and a resized image (thumb)

 

if it would be a problem of permissions, isn't both images supposed to have the same problem?

don't leave your original image untouched. Resize it, without resizing it just to make sure the permissions for your thumb, and main image match.

 

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.

don't leave your original image untouched. Resize it, without resizing it just to make sure the permissions for your thumb, and main image match.

 

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.

 

could you give me a code as example on how to do this.... its someone from this forum who helped me to build this code, i'm pretty much confused and i dont know what to modify

 

i would be very grateful if you have the time to do that

down here in your code:

 

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

 

 

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

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

 

add one more call to resample...ie

resample($target, $width, $height, $type, ?);

 

don't know what attr does, so you'll have to double check that. and you have to make sure that tartget matches your original uploaded file.

 

Should be good to go

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.