Uploading images Beginner help.

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
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Uploading images Beginner help.

Post by Addos »

I’m just embarking on learning how to upload (images) to a folder but I’m running into problems. The code below is from a Welling/Thompson book which lets you upload a text/plain file to a folder called ‘uploads’.
My problem is that whether I test locally or on a live server I keep getting the error “File is not plain text” and I can’t see why this is. I have tried replacing ‘text/plain’ to ‘images/jpeg’ but I still get the same error albeit an image this time.
If anyone can help me I’d be very grateful as I’m very much in the dark and I thought that if I could get this to work from a book first I could learn for this and then go on to implement similar into my page. But alas I can’t get over this first hurdle.
Thanks a mil for any advice
Brian

Code: Select all

<form ENCTYPE=&quote;multipart/form-data&quote; name=&quote;upload&quote; id=&quote;upload&quote;  action =&quote;upload.php&quote; method=&quote;post&quote;>
     <input type=&quote;hidden&quote;  name=&quote;MAX_FILE_SIZE&quote; value=&quote;1000000&quote;>
	 Upload Your File(s):<input name=&quote;userfile&quote; type=&quote;file&quote; >
     <input type=&quote;submit&quote; value=&quote;Send File&quote;>
   </form>

Code: Select all

<?PHP
if ($_FILES['userfile'] ['error'] > 0)
{
echo 'Problem: ' ;
switch ($_FILES['userfile'] ['error'])
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}
// Does the file have the right MIME type:
if ($_FILES['userfile']['type'] != 'text/plain')
{
echo 'problem: file is not plain text';
exit;
}

// put the file where we'd like it
$upfile ='/uploads/'.$_FILES['userfile'] ['name'] ;

if (is_uploaded_files($_FILES['userfile']['tmp_name']))
{
if(!move_uploaded_file($_FILES['userfile'] ['tmp_name'], $upfile))
{
echo 'Problem: Could not move file to destination ditectory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: ';
echo $_FILES['username'] ['name'];
exit;
}
echo 'File uploaded successfully <br> <br>';

// reformat the file contents
$fp = fopen($upfile. 'r');
$contents = fread ($fp, filesize ($upfile));
fclose ($fp);

$contents =strip_tags($contents);
$fp = fopen($upfile, 'w');
fwrite($fp, $contents);
fclose($fp);

// show what was uploaded
echo 'Preview of uploaded file contents: <br><hr>';
echo $contents;
echo '<br><hr>';
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's image/jpeg .. and that actually varies from browser to browser. Do not rely on the mime information supplied in $_FILES['userfile']['type'] to be accurate...

If you want to know what is being sent for a file (in that element), echo it.. it may vary from browser to browser. If you truely want only a text file, then you can create a filter that analyzes the file for binary characters. Even then, there are false positive chances.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks for you help.
As I’m hoping to just apply this image upload for a single client is it ok to still work under these conditions i.e. that once I know the specific browser that he/she is using I would be unlikely to run into problems. What would you guys do? Uploading images/word docs must be a common request I would imagine.
Thanks a again
Brian :wink:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I pass everything from a user through a validation and verification system. I don't care how small it is.
Post Reply