SeanB Posted July 20, 2009 Share Posted July 20, 2009 Hello, I am making a file uploader and it is almost complete. But I have constraints for file size and file extentions. But they do not report the error I want them to. Here is the code for the constraints: <?php $target = "files/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if ($uploaded_size > 10485760) { echo "Your file is too large.<br>"; $ok=0; } if ($uploaded_type =="text/php") { echo "Sorry, No PHP files allowed"; $ok=0; } if ($ok==0) { echo "Sorry your file was not uploaded"; } If you have a better method or you see the problem I am having and know how to fix it, Please tell me. Any help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/166584-file-upload-constraints/ Share on other sites More sharing options...
Alex Posted July 20, 2009 Share Posted July 20, 2009 $uploaded_size and $uploaded_type have no value. So of course it won't work. Also, for your $ok, why don't you use a boolean, and just use "true/false"? Would be easier. Additionally, you could just change your structure because the whole $ok, thing isn't even needed. The errors should be enough to notify the uploader that their file wasn't uploaded. You might want to look into filesize() and getimagesize() Quote Link to comment https://forums.phpfreaks.com/topic/166584-file-upload-constraints/#findComment-878407 Share on other sites More sharing options...
SeanB Posted July 20, 2009 Author Share Posted July 20, 2009 Well I don't know how to use those functions, I'm a begginer and this is my first script. But thanks for those links, I'll look at them. Quote Link to comment https://forums.phpfreaks.com/topic/166584-file-upload-constraints/#findComment-878409 Share on other sites More sharing options...
SeanB Posted July 20, 2009 Author Share Posted July 20, 2009 Okay, I have looked at those links but I still can't get it. If someone could edit my above code with what they think will make it work and send it back to me, that would be great! Quote Link to comment https://forums.phpfreaks.com/topic/166584-file-upload-constraints/#findComment-878555 Share on other sites More sharing options...
MatthewJ Posted July 20, 2009 Share Posted July 20, 2009 <?php $target = "files/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $uploaded_size = $_FILES['uploaded']['size']; $uploaded_type = $_FILES['uploaded']['type']; if ($uploaded_size > 10485760) { echo "Your file is too large.<br>"; } elseif ($uploaded_type =="text/php") { echo "Sorry, No PHP files allowed"; } else { echo "File Uploaded"; } Quote Link to comment https://forums.phpfreaks.com/topic/166584-file-upload-constraints/#findComment-878579 Share on other sites More sharing options...
waynew Posted July 20, 2009 Share Posted July 20, 2009 You should look into using the is_uploaded_file method as your script is open to abuse. Quote Link to comment https://forums.phpfreaks.com/topic/166584-file-upload-constraints/#findComment-878592 Share on other sites More sharing options...
SeanB Posted July 20, 2009 Author Share Posted July 20, 2009 @Matthew: Thanks, But I tried it and it didn't work. But to me your code lookes perfect. But I do have code writen after that, I'll post the whole php code at the end of this post. @Waynewex: I don't know what that is, I'm only a begginer. If you could expand on that I would appreciate it. PHP Code: <?php $target = "files/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $uploaded_size = $_FILES['uploaded']['size']; $uploaded_type = $_FILES['uploaded']['type']; if ($uploaded_size > 10485760) { echo "Your file is too large.<br>"; } elseif ($uploaded_type =="text/php") { echo "Sorry, No PHP files allowed"; } function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['uploaded']['name']) ; $ran = rand () ; $ran2 = $ran."."; $target = "files/"; $target = $target . $ran2.$ext; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "<b>Success!, Your file has been uploaded!</b><br /> <input type=\"text\" size=\"50\" value=\"http://www.example.com/$ran2$ext\">"; } else { echo "Sorry, there was a problem uploading your file."; } ?> The code uploads and displays correctly, But the constraints don't work. And yes I know there is 'www.example.com' at the end there, It's just that I don't want to give my url for that away. Quote Link to comment https://forums.phpfreaks.com/topic/166584-file-upload-constraints/#findComment-878604 Share on other sites More sharing options...
SeanB Posted July 21, 2009 Author Share Posted July 21, 2009 Bump. Please help guys, I really need your help! Quote Link to comment https://forums.phpfreaks.com/topic/166584-file-upload-constraints/#findComment-879422 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.