Jump to content

How to add restrictions on my little code?


lobski

Recommended Posts

Greetings.

 

I followed the PHP uploading tutorial, and I want to change a few things to it. First is to restrict the uploaded file to ONLY image files, such as jpeg, gif, png, etc.

 

I am following the tutorial here:

http://www.tizag.com/phpT/fileupload.php

 

So far I have this code:

 

 

 

<input type="hidden" name="MAX_FILE_SIZE" value="250000" />

Select image to upload:

<input name="uploadedfile" type="file" />

<input type="submit" value="Upload File" />

</form>

 

What would I have to change to restrict the files to images?

Edit function disabled on the forums?

 

Anyway, I pasted in the wrong one... Here's the correct code:

 

<?php

$target_path = "uploads/";

 

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

    echo "The file ".  basename( $_FILES['uploadedfile']['name']).

    " has successfully been uploaded.";

} else{

    echo "There was an error uploading the file, please try again!";

}

?>

<?php

$target_path = "uploads/";

 

if(getimagesize($_FILES['uploadedfile']['tmp_name'])) {

 

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

    echo "The file ".  basename( $_FILES['uploadedfile']['name']).

    " has successfully been uploaded.";

} else{

    echo "There was an error uploading the file, please try again!";

}

 

} else {

  die("Was not a Image!");

}

?>

 

You can give that a try, however I don't think it is right.

<?php

$target_path = "uploads/";

 

if(getimagesize($_FILES['uploadedfile']['tmp_name'])) {

 

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

    echo "The file ".  basename( $_FILES['uploadedfile']['name']).

    " has successfully been uploaded.";

} else{

    echo "There was an error uploading the file, please try again!";

}

 

} else {

  die("Was not a Image!");

}

?>

 

You can give that a try, however I don't think it is right.

 

I think that worked... Lastly, I would like to output the exact full URL of the file. Now I want to output 2 versions of the URL, a regular one, and a url with [/img] wrappers around it. How would I do this?  ???

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.