Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/166584-file-upload-constraints/
Share on other sites

$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()

<?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";
}

@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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.