Argh, host updated PHP. Can you see any problem please?

Soldato
Joined
2 May 2004
Posts
19,950
Hi,

I think my host updated PHP, now my script doesn't work, need to get it back asap.

Can you see anything wrong in this?:

Code:
function RandString(){
$keyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$length = 8;


$resultKey = "";
for ($i=0;$i < $length; $i++)
  $resultKey .= substr($keyChars, rand(1, strlen($keyChars) ), 1);
  
  return $resultKey;
}

$rands = RandString();

include "db.php";

if (empty($userfile)) {
	echo "There was no file entered or the file you tried to upload was too big. <br><a href='index.php'>Click here</a> to return to the index.";
} else {

$validMimes = array(
    'image/png' => '.png',
    'image/x-png' => '.png',
    'image/gif' => '.gif',
    'image/jpeg' => '.jpg',
    'image/pjpeg' => '.jpg',
	'image/bmp' => '.bmp'
);

if(!array_key_exists($_FILES['userfile']['type'], $validMimes)) {
    die('Sorry, but the file type you tried to upload is invalid; only images are allowed.');
}

After that code it goes onto putting the image in it's folder. The problem is that no matter what you'll get the echo after "if (empty($userfile)) {" displayed on the screen and the script won't go any further :(

Thanks very much in advance
Craig.
 
Last edited:
maybe using $null or something might make a difference?

if $userfile == $null { }
else {

... just a thought
 
I'm not sure why I need register globals? All I'm doing is letting a user upload an image, any user, it is then checked for the correct mime type and extension.

OK, I did this at the top:

Code:
$userfile = $_POST['userfile'];

if (!empty($userfile)) {
	echo ("There was no file entered or the file you tried to upload was too big. <br><a href='index.php'>Click here</a> to return to the index.");
} else {

Now all it's doing is showing the "if(!array_key_exists($userfile, $validMimes)) {" message :(

Craig.
 
Last edited:
Basically you now have to use $_GET / $_POST / $_FILES rather than just the form variable name ($userfile). You have later on in the script used $_FILES['userfile'] correctly, so I'd guess you just need to change $userfile to the correct format, too :)
 
I have tried that like this:

if (empty($_POST['userfile'])) {

and like this:

$userfile = $_POST['userfile'];

if (empty($userfile)) {

And they still give the same problem :(

Also, if I do this:
if (!empty($userfile)) {

(extra '!')

Then it goes on to display the message from
if(!array_key_exists($_FILES['userfile']['type'], $validMimes)) {
instead >.<
 
Last edited:
Still the same problem. :(

Edit:

Then if I remove the ! from the array_key_exists part and add it to the empty($userfile) part it goes through but doesn't upload it properly.
 
Last edited:
Output of the var_dumps:

Code:
array(2) {
  ["MAX_FILE_SIZE"]=>
  string(7) "1048600"
  ["submitfile"]=>
  string(6) "Upload"
}
array(1) {
  ["file"]=>
  array(5) {
    ["name"]=>
    string(8) "buzz.gif"
    ["type"]=>
    string(9) "image/gif"
    ["tmp_name"]=>
    string(14) "/tmp/phpdwkSvB"
    ["error"]=>
    int(0)
    ["size"]=>
    int(4978)
  }
}

Dj_Jestar said:
Have a read of the manual for file uploads..

http://www.php.net/features.file-upload

Mine looks practically like that, with a few minor changed and additions.
 
Last edited:
The form:

Code:
<form action="upload.php" method="post" enctype="multipart/form-data">
            <input type="hidden" name="MAX_FILE_SIZE" value="1048600">
            <span class="style1"><br>Press browse, select your image and click 'Upload'</span>  <br>
  <input name="file" type="file" size="50">  
  <input type=SUBMIT name="submitfile" value="Upload">
          </form>

So userfile is right >.<

Edit:

Actually, I just noticed it isn't, that's weird, it always was from what I remeber.
 
Damn, stupid mistake by me there!
Thanks for the help and sorry for all the trouble over me being stupid :)

Thank v much, it works now.
Craig.
 
Back
Top Bottom