Upload Form Extra's ?????

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Upload Form Extra's ?????

Post by JustinMs66 »

i have a working PHP upload form script, but i want to have it so once the file is uploaded, it displays code that says like "if you want to link this in a forum, use this code:" and it displays the code. but like rite now, when i try that, it actually DOES the code, instead of displaying it, it uses it. like it makes the link, istead of displaying the code for the link.

i also want it to try and recognize if it's an image, and if it IS, display the code for putting the IMAGE on a forum.

so how would i display that? use a form or something?

here is my working code for just the upload. all it displays after the upload is, "here is your link: the_link"

Code: Select all

<?php
$bad_types = array('application/octet-stream','application/x-msdos-program');
if( in_array( $_FILES['uploadedfile']['type'], $bad_types ) )
{
    echo "That File type is not supported.";
}
else
{
$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 been uploaded. here is the link to your file: <a href=uploads/".  basename( $_FILES['uploadedfile']['name']). ">".  basename( $_FILES['uploadedfile']['name'])."</a>";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
}
?>
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

To display <a href...> as is rather than a link; replace the < with &#60 and the > with &#62 - those are html character entities, and are not parsed as html tags, but outputted to the browser as <>!

http://www.ilovejackdaniels.com/cheat-s ... eat-sheet/ has the rest of them!

To find out if it is an image use a function like

Code: Select all

<?php
function jpgpdfcheck($filecheck){ //returns true if file supplied is a PDF
$imginfo=getimagesize($filecheck); 
if ($imginfo[2]==2){//file is a jpeg
return true;
}
?>
that returns true if it is a jpeg. Look in the manual (for getimagesize()) for the numbers for gif, png
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

andym01480 wrote:To display <a href...> as is rather than a link; replace the < with &#60 and the > with &#62 - those are html character entities, and are not parsed as html tags, but outputted to the browser as <>!

http://www.ilovejackdaniels.com/cheat-s ... eat-sheet/ has the rest of them!
htmlentities() and htmlspecialchars() make that a bit easier.
andym01480 wrote:To find out if it is an image use a function like

Code: Select all

<?php
function jpgpdfcheck($filecheck){ //returns true if file supplied is a PDF
$imginfo=getimagesize($filecheck); 
if ($imginfo[2]==2){//file is a jpeg
return true;
}
?>
that returns true if it is a jpeg. Look in the manual (for getimagesize()) for the numbers for gif, png
Because getimagesize() will fire a warning if the given file isn't an image (and return false, I'd go with a tiny bit smarter version if this is the direction:

Code: Select all

<?php
function jpgpdfcheck($filecheck){ //returns true if file supplied is a PDF
$err = error_reporting(0);
$imginfo=getimagesize($filecheck);
error_reporting($err);
return is_array($imginfo) && $imginfo[2] == 2;
?>
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Thanks feyd, didn't realise it would fire a warning!

Oh and loose the pdfcheck line which calls another function that checked the uploaded file was a pdf!
Post Reply