We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,348 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Issue with image management

This could be a PHP issue or a linux issue.

My form uses the following code to allow users to upload their banner and worked fine on my old server:

             if ($_FILES["banner"]["name"]!="")
            {
                $folder_path = "images/server_banners/";
                $myfileext=substr($_FILES["banner"]["name"], - 4,4);
                $banner_file_path=$username."-".$server_id.$myfileext;
                $file_path = $folder_path.$banner_file_path; 
                    if (file_exists($file_path))
                      {
                            unlink($file_path);
                            move_uploaded_file($_FILES["banner"]["tmp_name"],$file_path);
                      }
                     else
                      {
                          move_uploaded_file($_FILES["banner"]["tmp_name"],$file_path);
                      }

$sql = mysql_query("UPDATE servers SET banner='".$banner_file_path."' WHERE id = '".$server_id."' and administrator = '".$_SESSION['valid']."'");
                }

But it seems the images now cannot be overwritten? When I submit the form everything is fine as I can see the browser uploading the image by percentage - however the image is not stored IF a file already exists with the same name. Which means the script is working fine but there is some sort of permissions issue?

Just for a test I gave all folders and files 777 and the image still wasn't overwritten.

I then deleted the stored file and reverted back permissions and uploaded a new file and it worked fine.

Any ideas? Is there a way to ensure files can be overwritten in a certain directory?

3
Contributors
17
Replies
23 Hours
Discussion Span
7 Months Ago
Last Updated
18
Views
mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

When you uplode files PHP display any error?

radow
Junior Poster in Training
73 posts since Oct 2012
Reputation Points: 7
Solved Threads: 18
Skill Endorsements: 0

Who owner folder in which you upload files? Must be apache user("www-data" or "apache" or etc.).

radow
Junior Poster in Training
73 posts since Oct 2012
Reputation Points: 7
Solved Threads: 18
Skill Endorsements: 0

No errors. Some are www-data and some are root

mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

If try method unlink() wrap in construction IF

if(unlink($path_file))
{
    move_uploaded_file($_FILES["banner"]["tmp_name"],$file_path);
}
else
{
    echo "ERROR";
}
radow
Junior Poster in Training
73 posts since Oct 2012
Reputation Points: 7
Solved Threads: 18
Skill Endorsements: 0

And try to add in your script:

error_reporting(E_ALL);

because when method unlink() have error will be generated error level E_WARNING

radow
Junior Poster in Training
73 posts since Oct 2012
Reputation Points: 7
Solved Threads: 18
Skill Endorsements: 0

Only error that comes back is

SMTP -> ERROR: Failed to connect to server: Connection refused (111) 
Language string failed to load: smtp_connect_failed Mailer Error: Language string failed to load: smtp_connect_failed

and thats because I haven't finished configuring my SMTP on the page yet.

The file wasn't being replaced but the upload still works if I delete the existing file first.

Thanks for your help so far!

mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

Error reporting is already on fully

mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

maybe this will show useful information

radow
Junior Poster in Training
73 posts since Oct 2012
Reputation Points: 7
Solved Threads: 18
Skill Endorsements: 0

wait wait wait it's working! bravo!

Your if statement worked! thank you

mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

I'm having the same issue with another script - how can I apply your solution to this:

<?php
session_start();

$id=isset($_GET['id']) ? $_GET['id'] : 0;
$server_name=isset($_GET['server_name']) ? $_GET['server_name'] : 0;
$ip=isset($_GET['ip']) ? $_GET['ip'] : 0;
$cur_players=isset($_GET['cur_players']) ? $_GET['cur_players'] : 0;
$max_players=isset($_GET['max_players']) ? $_GET['max_players'] : 0;
$vote_count=isset($_GET['vote_count']) ? $_GET['vote_count'] : 0;
$uptime=isset($_GET['uptime']) ? $_GET['uptime'] : 0;
$pingstatus=isset($_GET['pingstatus']) ? $_GET['pingstatus'] : 0;

$players=$cur_players." / ".$max_players;

        // Create image instances
        $src = imagecreatefromjpeg('../images/banners/server_banner.jpg');
        $dest = imagecreatetruecolor(450, 100);

        // Copy
        imagecopy($dest, $src, 0, 0, 0, 0, 450, 100);

        // Create some colors
        $heading_color = imagecolorallocate($dest, 255, 255, 255);
        $data_color = imagecolorallocate($dest, 0, 0, 0);

        //imagefilledrectangle($dest, 0, 0, 399, 29, $white);

        // The text to draw
        $text1 = 'Server: ';
        $text2 = 'IP: ';
        $text3 = 'Players: ';
        $text4 = 'Votes: ';
        $text5 = 'Uptime: ';
        $text6 = 'Status: ';
        // Replace path by your own font path
        //$font = 'css/fonts/MINECRAFT_Z2FONT.TTF';
        $font = '../css/fonts/merriweather-regular-webfont.ttf';


        // Add some shadow to the text
        //imagettftext($dest, 12, 0, 11, 21, $grey, $font, $text);

        // Add the text
        imagettftext($dest, 10, 0, 20, 22, $heading_color, $font, $text1);
        imagettftext($dest, 10, 0, 20, 44, $heading_color, $font, $text2);
        imagettftext($dest, 10, 0, 20, 65, $heading_color, $font, $text3);
        imagettftext($dest, 10, 0, 20, 86, $heading_color, $font, $text6);
        imagettftext($dest, 10, 0, 260, 65, $heading_color, $font, $text4);
        imagettftext($dest, 10, 0, 260, 85, $heading_color, $font, $text5);

        imagettftext($dest, 10, 0, 78, 22, $data_color, $font, $server_name);
        imagettftext($dest, 10, 0, 78, 44, $data_color, $font, $ip);
        imagettftext($dest, 10, 0, 78, 65, $data_color, $font, $players);
        imagettftext($dest, 10, 0, 78, 86, $data_color, $font, $pingstatus);
        imagettftext($dest, 10, 0, 320, 65, $data_color, $font, $vote_count);
        imagettftext($dest, 10, 0, 320, 86, $data_color, $font, $uptime."%");
        //imagettftext($dest, 10, 0, 410, 83, $data_color, $font, "%");

        // Output and free from memory
        header('Content-Type: image/jpeg');

        // Save the image
        imagejpeg($dest, '../images/banners/'.$id.'.jpg',100);
        imagejpeg($dest);

        //destroy memory
        imagedestroy($dest);
        imagedestroy($src);


?>

Thanks!

mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

Sorry, I do not understand what you have a problem and what is your question?

radow
Junior Poster in Training
73 posts since Oct 2012
Reputation Points: 7
Solved Threads: 18
Skill Endorsements: 0

I have the same issue with the code above. The code above takes an image and writes data on top using PHP's imagegettftext. The process works fine but when it comes to overwriting the existing image it fails. I don't know where to apply your solution which fixed the same error earlier in this post.

mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

write this instead 62 and 63 row

$file_path = '../images/banners/'.$id.'.jpg';
if(file_exists($file_path))
{
    unlink($file_path);
}

// Save the image
imagejpeg($dest, $file_path,100);
radow
Junior Poster in Training
73 posts since Oct 2012
Reputation Points: 7
Solved Threads: 18
Skill Endorsements: 0

You're a god :)

mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

Really nice conversation betwen you both and help me to in my work and also for others...so in cas eof i do any mistake please help me for give me suport.

ericfox
Newbie Poster
5 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Sorry?

mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

What happened?

radow
Junior Poster in Training
73 posts since Oct 2012
Reputation Points: 7
Solved Threads: 18
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1135 seconds using 2.73MB