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);
}
}
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.
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.
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")) {
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...
PHP does only work on a server with PHP enabled, PHP won't run on your computer itself.
PHP is a server-side language!
精彩评论