Uploading images Beginner help.
Posted: Tue Apr 05, 2005 1:20 pm
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
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="e;multipart/form-data"e; name="e;upload"e; id="e;upload"e; action ="e;upload.php"e; method="e;post"e;>
<input type="e;hidden"e; name="e;MAX_FILE_SIZE"e; value="e;1000000"e;>
Upload Your File(s):<input name="e;userfile"e; type="e;file"e; >
<input type="e;submit"e; value="e;Send File"e;>
</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>';
?>