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?

When you uplode files PHP display any error?

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

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

If try method unlink() wrap in construction IF

if(unlink($path_file))
{
    move_uploaded_file($_FILES["banner"]["tmp_name"],$file_path);
}
else
{
    echo "ERROR";
}
commented: Perfect solution, thanks! +2

And try to add in your script:

error_reporting(E_ALL);

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

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!

Error reporting is already on fully

maybe this will show useful information

wait wait wait it's working! bravo!

Your if statement worked! thank you

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!

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

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.

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);
commented: perfect! +0

You're a god :)

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.

What happened?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.