Jump to content


Photo

File Upload help...


  • Please log in to reply
10 replies to this topic

#1 WhyWhyZed

WhyWhyZed
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 April 2006 - 04:38 AM

I'm quite new to coding, both HTML and PHP. I took the responsibility upon myself to manage and maintain a site that would be used for another discussion forum's meet/get together. Well, I never really had a chance to do all of what I wanted to do, but I would really like to do this one thing.

Not too many of the people have webspace, and alot of the others can be a hassle. But not just that, it'd fit the site more appropriately. So i'd like to have a file upload system for them to upload photos and movies.

I found a great tutorial on another site, and it was as simple as copying and pasting with just a few minor adjustments. However, when I test it... after clicking the "upload file" button I always get
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]"Sorry, wrong file type, "image/pjpeg" is not allowed. "[/quote]

This happens with any file. Can someone please look over my code, and help me out!, as well as explain to me what the error was. I really am trying to learn. I read through the whole coding and tried to analyze it for the future.

[a href=\"http://www.yyzmeet.org/test.php\" target=\"_blank\"]http://www.yyzmeet.org/test.php[/a]

Thanks alot for any advice an input that is given. Nothing ever gets done un-noticed.

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 April 2006 - 05:04 AM

you need to post your code in order for us to look over it. remember, php is parsed on the server. the link you provided shows us the finished product but not the php code, even in "view source"

but umm, here is a really super simple upload script i made a long time ago. i made it as 2 files but you can always condense it to 1 file or whatever:

file 1: uploader.html

<html>
<body>
select a file to upload, biatch!<br>
<form action="uploader.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50"><br>
<input type="submit" value="spank me!">
</form>
</body>
</html>

file 2: uploader.php

?php
if($file_name !=" ")
{
copy ("$file","$file_name") or die("could not upload your shit, man");
}
else { die("no file specified, moron"); }
?>

<html>
<body>
File upload succeeded...<br>
<ul>
<li>sent: <?php echo "$file_name"; ?>
<li>size: <?php echo "$file_size"; ?>
<li>type: <?php echo "$file_type"; ?>
</body>
</html>


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 WhyWhyZed

WhyWhyZed
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 April 2006 - 05:21 AM

<?PHP
//If the submit button is pressed:
if($_POST['submit']){

//category you would like to upload files to
$directory = "/httpdocs/0406meetphotos/";

//max file size
$max_file_size = "20480000";

//allowed files types users can upload
$allowedfile[] = "image/gif"; //.gif
$allowedfile[] = "image/jpeg"; //.jpeg
$allowedfile[] = "image/jpg";  //.jpg
$allowedfile[] = "video/x-ms-wmv";  //.wmv
$allowedfile[] = "video/x-msvideo";  //.avi
$allowedfile[] = "video/mpeg";  //.mpeg

//Check max file size
if (is_uploaded_file($_FILES["file"]["tmp_name"])) {
    if($_FILES["file"]["size"]>$max_file_size) {
        $is_uploaded = "failed";
        echo 'Sorry, this file is too large. The maximum filesize is '.$max_file_size.' bytes, although your file is '.$_FILES["file"]["size"].'. ';
        exit();  //If $is_upload = failed, then we stop the uploading process
    }

    //Check file type
    if(!in_array($_FILES["file"]["type"],$allowedfile)) {
        $is_uploaded = "failed";
        echo 'Sorry, wrong file type, "'.$_FILES["file"]["type"].'" is not allowed. ';
        exit();  //If $is_upload = failed, then we stop the uploading process
    }

    //Check to see if file exists | If it does, then we stop the process, although if it doesnt then we continue
    if(file_exists($directory.$_FILES["file"]["name"])) {
        $is_uploaded = "failed";
        echo 'Sorry, this file already exists. ';
        exit();  //If $is_upload = failed, then we stop the uploading process
    }
    
    //Now, if $is_uploaded does NOT = failed, we remove invalid characters in the filename, and replace all spaces with underscores
        if($is_uploaded!="failed") {
                $replace = array("$","%","#","@","!","&","^","*","(",")","-");
                $new = str_replace($replace,"",$_FILES["file"]["name"]);
                $fileName = str_replace(" " , "_" , $new);

                        //If the directory defined in the beggining does not exist, we create it and CHMOD it to 777
                        if(! is_dir($directory)){
                                mkdir($directory,0777);
                        }
                                                //upload the file and check for errors
                        if (move_uploaded_file($_FILES["file"]["tmp_name"], $directory.$fileName)) {
                                echo "Your file, ". $fileName ." has successfully been uploaded!  Click <a href=\"".$directory.$fileName."\">Here</a> to view your file.";
                        }
        else {
                echo 'Sorry, your file has not uploaded.';
                exit();
                }
        }
} else {
        echo 'There has been an unknown error while uploading';
        exit();
}
}
?>


<table width='90%' cellpadding='0' cellspacing='0' style='border:1px solid #000000;'>
        <tr>
                <td colspan="2" style='border-bottom:1px solid #000000;padding:3px;padding-left:6px;'>Upload File</td>
        </tr>
        <tr>
                <td>
<center>        <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">          <table>            <tr>              <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Choose File:</font></td>              <td><input type="file" name="file">                <input name="submit2" type="submit" value="Upload File"></td>              <input type="hidden" name="submit" value="true">            </tr>            </table>        </form>      </center>


#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 April 2006 - 05:40 AM

well first off i notice in your original post that the error says

"Sorry, wrong file type, "image/[!--coloro:red--][span style=\"color:red\"][!--/coloro--]p[!--colorc--][/span][!--/colorc--]jpeg" is not allowed."

notice that [!--coloro:red--][span style=\"color:red\"][!--/coloro--]p[!--colorc--][/span][!--/colorc--] in there and then look in your code under the allowed file types:

//allowed files types users can upload
$allowedfile[] = "image/gif"; //.gif
$allowedfile[] = "image/jpeg"; //.jpeg
$allowedfile[] = "image/jpg"; //.jpg
$allowedfile[] = "video/x-ms-wmv"; //.wmv
$allowedfile[] = "video/x-msvideo"; //.avi
$allowedfile[] = "video/mpeg"; //.mpeg

you will notice that "image/pjpeg" so does the code work for the file types in the list? maybe you are simply trying a file that is not on that list. try uploading a file that has .gif .jpeg .jpg extension for example or add another line to it

$allowedfile[] = "image/pjpeg"; //.pjpeg

and try uploading the same file.

if this does not work, then you can also remove this part:

//Check file type
    if(!in_array($_FILES["file"]["type"],$allowedfile)) {
        $is_uploaded = "failed";
        echo 'Sorry, wrong file type, "'.$_FILES["file"]["type"].'" is not allowed. ';
        exit();  //If $is_upload = failed, then we stop the uploading process
    }

from your code. this will remove checking the file type altogether, if you aren't worried about that..

these are just a few quick and dirty solutions you can try and does not necessarily fix the actual bug.

edit: i don't think any of that is really gonna help. i went to your example linkie and tried to upload a legit file that's on that list and it passes the legit filetype test. but it keeps spitting out

'Sorry, your file has not uploaded.'

so you probably were trying to upload an invalid filetype and the bug is somewhere after. i'll keep looking.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 WhyWhyZed

WhyWhyZed
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 April 2006 - 01:20 PM

Thanks for your help, so far, Crayon Violent.

What I don't understand is that it's just a regular jpg. Where is the "P"coming from? Was it because it was saved from Photoshop? Well, I know this is the PHP forum, but that's confusing me too.

I am absolutely stumped on this one, completely. :(

Anyone else know the problem?

EDIT: Well I just added the pjpeg to the file list. So we overcame that, but as you said...I still get the "Sorry, file not uploaded" Error.

#6 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 28 April 2006 - 11:55 PM

You might be having errors with your directory name:

$directory = "/httpdocs/0406meetphotos/";

Your leading slash is telling your script to start in the root directory.

Just for fun, you might add the following just below the original if(! is_dir... in your code:

if(! is_dir($directory)){
     mkdir($directory,0777);
}
if(! is_dir($directory)){
     die("directory does not exist after attempting to create it.\n<br>try using the following as your starting point:\n<br>".realpath("."));
}


#7 WhyWhyZed

WhyWhyZed
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 29 April 2006 - 03:09 AM

[!--quoteo(post=369758:date=Apr 28 2006, 07:55 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Apr 28 2006, 07:55 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You might be having errors with your directory name:

$directory = "/httpdocs/0406meetphotos/";

Your leading slash is telling your script to start in the root directory.

Just for fun, you might add the following just below the original if(! is_dir... in your code:
[/quote]

You're the best!! I really appriciate it. You had some humor with it too. I added the code, tried to upload a file, and it told me what to do. It was quite funny.

So I did some tweaking, and then the URL that would appear with the address was messed up, so I solved that myself. But when I enter
target="_blank"
it doesn't work.

This should be a simple solution now for all you geniuses :-D


if (move_uploaded_file($_FILES["file"]["tmp_name"], $directory.$fileName)) {
                                echo "Your file, ". $fileName ." has successfully been uploaded!  Click <a href=\"".$directory2.$fileName."\" >Here</a> to view your file.";
                        }


#8 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 29 April 2006 - 03:30 AM

if (move_uploaded_file($_FILES["file"]["tmp_name"], $directory.$fileName)) {
                                echo "Your file, ". $fileName ." has successfully been uploaded!  Click <a href=\"".$directory2.$fileName."\" target=\"_blank\">Here</a> to view your file.";
                        }

just escape your quotes.

#9 WhyWhyZed

WhyWhyZed
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 29 April 2006 - 03:35 AM

[!--quoteo(post=369791:date=Apr 28 2006, 11:30 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Apr 28 2006, 11:30 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
if (move_uploaded_file($_FILES["file"]["tmp_name"], $directory.$fileName)) {
                                echo "Your file, ". $fileName ." has successfully been uploaded!  Click <a href=\"".$directory2.$fileName."\" target=\"_blank\">Here</a> to view your file.";
                        }

just escape your quotes.
[/quote]

Thanks again. Can you by any chance try uploading a large photo file? I don't know if it's my internet or the coding but everytime I try and upload a 1mb+ file... I get cannot display page.

#10 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 29 April 2006 - 03:49 AM

seems to be working for me:
[a href=\"http://www.yyzmeet.org/0406meetphotos/bar.jpg\" target=\"_blank\"]http://www.yyzmeet.org/0406meetphotos/bar.jpg[/a] 1,177,888 bytes (1.1mb)
[a href=\"http://www.yyzmeet.org/0406meetphotos/bar2.jpg\" target=\"_blank\"]http://www.yyzmeet.org/0406meetphotos/bar2.jpg[/a] 2,010,995 bytes (1.9mb)

#11 WhyWhyZed

WhyWhyZed
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 29 April 2006 - 03:55 AM

Grrrrr... I hate my internet connection. :-)

Nice bar, btw. And a final Thank you for your help.

P.S - The links will be dead soon.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users