Cory
Cory

Reputation: 742

Script works on computer but not server?

Strange issue... I have no idea why this isn't working on my server.

For some reason the IF's aren't working.

The major one thats puzzling me is the one that checks the file extension. Even if it doesn't match for some reason its returning TRUE on my server and not my computer.

    if (isset($_POST['submit'])) {

    $sizelimit = 50; //File size limit in KB.
    $destination_folder = 'images/user_avatars/temp/';

    $fileurl = $_POST['avatar_url'];
    $filetype = substr(strrchr($fileurl,'.'),1);
    $filename = "user_" . $_SESSION[user_id] . "." . $filetype;

    $temp = $destination_folder . $filename;

    if ($filetype == "gif" || "png" || "jpg" || "jpeg") {

        $file = fopen ($url, "rb");
        if ($file) {
            $newfile = fopen ($temp, "wb");
            if ($newfile) {
                while(!feof($file)) {
                    fwrite($newfile, fread($file, 1024 * 8 ), 1024 * 8 );
                }
            }
        }

        if ( filesize($temp) > 1024 * $sizelimit ) {
            $errors .= "The file size of the image you entered is too large!";
        }
    } else {
        $errors .= "The URL you entered is not a valid image!";
    }

    if (isset($errors)) {
        $smarty->assign("error_message", $errors);
        unlink($temp);
    } else {
        $avatar_sql = "
            UPDATE
                users
            SET
                user_avatar = '$filename'
            WHERE
                user_id = '$_SESSION[user_id]'
        ";
        $avatar_query = @mysqli_query ($db_connect, $avatar_sql);

        fclose($file);
        fclose($newfile);
    }
}

Upvotes: 0

Views: 152

Answers (5)

seymar
seymar

Reputation: 4063

PHP does only work on a server with PHP enabled, PHP won't run on your computer itself.
PHP is a server-side language!

Upvotes: 0

Andrzej Doyle
Andrzej Doyle

Reputation: 103797

I'm no PHP guru, but I'm pretty sure this is wrong:

if ($filetype == "gif" || "png" || "jpg" || "jpeg")

It's almost certainly the case that the == operator bounds more tightly than ||. In other words, what you have is:

IF
   $filetype == "gif" 
OR
   "png"
OR
   "jpg"
OR
   "jpeg"

and I'm further going to assume that a non-empty string gets coerced to true in a boolean context, so the latter OR conditions are always true.

Now this should have exhibited this behaviour on your machine too, so I suggest that you've probably uploaded a different file to the one you were testing against locally. This could be down to browser caches, multiple working directories, any kind of thing like that. Speaking from experience, it's surprisingly easy to do...

Upvotes: 1

Alex
Alex

Reputation: 14618

if ($filetype == "gif" || "png" || "jpg" || "jpeg") {

you are doing it wrong. Read about operators in PHP.

The shorter right way to do this is:

if(in_array($filetype, array("gif","png","jpg","jpeg")) {

Upvotes: 1

netcoder
netcoder

Reputation: 67695

It shouldn't work either way. What you are evaluating right now is:

if ($filetype == "gif" || "png" == true || "jpg" == true || "jpeg" == true) ...

"png" is always == true, so is "jpg" and "jpeg".

It should be:

if ($filetype == "gif" || $filetype == "png" || $filetype == "jpg" || $filetype == "jpeg") ...

Or using in_array is actually clearer.

Upvotes: 1

pestaa
pestaa

Reputation: 4749

if ($filetype == "gif" || "png" || "jpg" || "jpeg")

This is why your script is unreliable. Use this instead:

if ($filetype == "gif" || $filetype == "png" || $filetype == "jpg" || $filetype == "jpeg" )

or even better:

if (in_array($filetype, array("gif", "png", "jpg", "jpeg))

Not a single interpreter will be smart enough to think instead of you. You need to precisely tell it what you mean.

Upvotes: 6

Related Questions