Questions about the $_FILES array...

Discussion and Questions about PHP.
jnoelcook
Posts: 6
Joined: Sat Apr 14, 2007 7:10 pm

Questions about the $_FILES array...

Postby jnoelcook » Sun Apr 15, 2007 10:46 am

All,

I started working with PHP a year ago. I am building some functionality to my website where users can upload Rich Text and Rich Text only files to the site. First question is how do I limit the files to be only Rich Text files. I only have found code for limiting the uploads to images. This is what I have found. It would be great if someone could give me the variation to limit it to what I need.

if(!ereg("image",$_FILES['pix']['type']))
{
echo "<b>File is not plain text.</b><br>";
include("submit_article.php");
exit();
}

Secondly, I have not even mastered the art of being able to upload a photo. When a user selects his or her image, the file gets placed in the temp just fine; this is obvious because larger files take longer to accomplish this. However, it never does get placed into the folder. It just disappears. I am assuming I have the path (or the code surrounding the path) incorrect. I spoke with my host and they suggested using the absolute path. Tried that, but it doesn't help. This is the line I am curretly using, though I have tried many variations:

These lines follow what I included above...

else
{
$destination = '/submissions'."/".$_FILES['pix']['name'];
$temp_file = $_FILES['pix']['tmp_name'];
move_uploaded_file($temp_file,$destination);
header("Location: htdocs/reviewers/home.php");
}

For security reasons, I have removed all of the path in this example except for the /submissions part. In any case, is the rest of the line correct?

Now, in the book, "PHP & MySQL for Dummies," I do not understand the following lines. Perhaps someone in the forum can help. I have written Janet Valade, the author, but she did not reply to my questions. Here are the lines in question...

$_FILES['fieldname']['name']
$_FILES['fieldname']['type']
$_FILES['fieldname']['tmp_name']
$_FILES['fieldname']['size']

So the array is $_FILES, but is the fieldname the same for all these? Does PHP already detect the file type and name, etc., or do I actually need to put these lines in my code. I hope I am being clear with my questions, as I understand nothing about this, and have been stumped on this coding problem for almost a year now and have been unable to move forward with the functionality of my website because no one seems willing to answer these questions and help me through this functionality.

Basically, I just want users to be able to upload Rich Text files to my site. The above info is what I know so far, which is limited. Can someone please help.

Thanks greatly,

Jeff

jnoelcook
Posts: 6
Joined: Sat Apr 14, 2007 7:10 pm

Narrowed down the problem...

Postby jnoelcook » Mon Apr 16, 2007 2:43 pm

Okay, all, I've made some headway and narrowed down this issue. It is in this line of code...

if($_FILES['rtf']['tmp_name'] == "none")

Could someone share with me the proper code for limiting the file upload to .rtf only?

Thanks,

Jeff

jnoelcook
Posts: 6
Joined: Sat Apr 14, 2007 7:10 pm

Wrong line...

Postby jnoelcook » Mon Apr 16, 2007 3:07 pm

Oops, I included the wrong line of code. The problem is actually in this line...

if(!ereg(".rtf",$_FILES['rtf']['type']))

Sorry about any confusion.

Jeff

jnoelcook
Posts: 6
Joined: Sat Apr 14, 2007 7:10 pm

A bit further...

Postby jnoelcook » Mon Apr 16, 2007 4:08 pm

Okay, I better understand the nature of a built in array after doing some further reading. And I was able to bypass the limit by switching temporarily to limit the uploads to images with the following code:

if(!ereg("image",$_FILES['rtf']['type']))

I will wait for you guys for the proper language within the quotes in place of "image" for .rtf filetypes.

Any help with this is greatly appreciated.

Thanks,

Jeff

User avatar
admin
Site Admin
Posts: 1303
Joined: Mon Oct 24, 2005 2:07 am
Location: Stuart, FL
Contact:

Re: A bit further...

Postby admin » Mon Apr 16, 2007 7:16 pm

Okay, I better understand the nature of a built in array after doing some further reading. And I was able to bypass the limit by switching temporarily to limit the uploads to images with the following code:

if(!ereg("image",$_FILES['rtf']['type']))

I will wait for you guys for the proper language within the quotes in place of "image" for .rtf filetypes.

Any help with this is greatly appreciated.

Thanks,

Jeff
Jeff,

This is a IF/THEN that should do the trick for you. I wrote this for a news program I did.

It allows both jpg and gif images. Modify it to suit your needs.

This is just part of a larger function. This part just handles the actual image uploads.

Admin Chuck

if(($_FILES[images][type][$key] != "image/jpg") AND ($_FILES[images][type][$key] != "image/gif"))
{
echo "Status: <b>$filename</b> not uploaded. File type not allowed.<br>";
}
else
{
$add = "$uploaddir/$filename"; // upload directory path is set
copy($_FILES[images][tmp_name][$key], $add); // upload the file to the server
chmod("$add",0777); // set permission to the file.
echo "Status: <b>$filename</b> Uploaded.<br>";
}

Replace the image/* MIME type with: application/rtf. Replace the [images] with whatever name you give the input field.
Last edited by admin on Mon Apr 16, 2007 7:57 pm, edited 1 time in total.

jnoelcook
Posts: 6
Joined: Sat Apr 14, 2007 7:10 pm

Thanks...

Postby jnoelcook » Mon Apr 16, 2007 7:38 pm

Great, I appreciate it.

I now have the files uploading good as text files. With your code, can I just swap out "image/jpg" for "text/rtf" and limit it to rich text files?

Jeff

User avatar
admin
Site Admin
Posts: 1303
Joined: Mon Oct 24, 2005 2:07 am
Location: Stuart, FL
Contact:

Re: Thanks...

Postby admin » Mon Apr 16, 2007 7:58 pm

Great, I appreciate it.

I now have the files uploading good as text files. With your code, can I just swap out "image/jpg" for "text/rtf" and limit it to rich text files?

Jeff
See the last line of my previous post.

Chuck

jnoelcook
Posts: 6
Joined: Sat Apr 14, 2007 7:10 pm

Oops...

Postby jnoelcook » Tue Apr 17, 2007 9:36 am

Sorry, Chuck. I am blind sometimes. :)

Thanks so much for your help. I will get this going today.

Jeff


Return to “PHP Q&A”

Who is online

Users browsing this forum: No registered users and 1 guest

cron