Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

another file upload question 1

Status
Not open for further replies.

secretsquirrel

Programmer
Mar 22, 2001
202
GB
sorry, for adding another file upload question, but i can't find my answer on this site.

all i want to do is upload an image file using php. i've got my file upload page working, but at the moment it only allows me to upload .txt files!

i've tried using $_FILES['myFile']['name'] and the different variations of this to see what information is actually coming through and the page finds the file name and understands the file type (image/pjpeg), but gives a file size of 0.

i was wondering if there's a php.ini variable i need to change or if there's any reason why .txt files are fine, but anything else isn't.

let me know if you need me to post some code.

thanks in advance,

ss...
 
It may be your form tag does it look like this
Code:
<form enctype=&quot;multipart/form-data&quot; action=&quot;myscript.php&quot; method=&quot;post&quot;>
Posting some coe might help us understand the problem.

MrBelfry
 
ok, here's the relevant code...

my form...
-----------------------------------------------

<form enctype=&quot;multipart/form-data&quot; action=&quot;addImageForm.php&quot; method=&quot;post&quot;>
<input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;1000&quot;>
Upload this file: <input name=&quot;userfile&quot; type=&quot;file&quot;>
<input type=&quot;Submit&quot; value=&quot;Send File&quot;>
</form>

-----------------------------------------------


and the page called by the form...
-----------------------------------------------

if ($userfile == &quot;none&quot;)
{
print &quot;Problem: no file uploaded&quot;;
exit;
}

if ($userfile_size == 0)
{
print &quot;Problem: uploaded file is zero length&quot;;
exit;
}

if (!is_uploaded_file($userfile))
{
print &quot;Problem: possible file upload attack&quot;;
exit;
}

$upfile = &quot;$valid_user/&quot;.$userfile_name;

if (!copy($userfile, $upfile))
{
print &quot;Problem: could not move file into directory&quot;;
exit;
}

print &quot;File uploaded successfully<br><br>&quot;;
$fp = fopen($upfile, &quot;r&quot;);
$contents = fread($fp, filesize($upfile));
fclose($fp);

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

print &quot;Preview of uploaded file contents:<p>&quot;;
print $contents;

-----------------------------------------------

if i try and upload a .txt file it works fine and displays the contents of the file. however, if i try any other file type it gets as far as the first if() statement and prints &quot;Problem: no file uploaded&quot; in the browser.

the script is originally taken from an example in a book and was designed for uploading text files, but i don't see how file type should affect it.

any ideas?

ss...
 
sleipnir214,

using your code, i get the following in my browser...

Array
(
[userfile] => Array
(
[name] => Image4.jpg
[type] => image/pjpeg
[tmp_name] => none
[size] => 0
)

)

so, it's obviously finding a file name and file type, but it doesn't actually find any file contents. could this be to do with the way i'm handling the file while it's on it's way to the server?

still confused,

ss...
 
What is the setting for &quot;file_uploads&quot;?

What is the size of the file?

How does that filesize compare to your php.ini configuration directives &quot;upload_max_filesize&quot; and &quot;post_max_size&quot;?

What is the setting for &quot;upload_tmp_dir&quot;? How does free space on that drive's filesystem compare to the size of the file?


Want the best answers? Ask the best questions: TANSTAAFL!!
 
1. file_uploads = On

2. file size will vary, but the one i am testing with is 18kb

3. upload_max_filesize = 2M
post_max_size = 8M

4. upload_tmp_dir = temp (which exists on the server)
and there's plenty of freespace

ss...
 
Are you using the form that you posted earlier? The one which includes the field:

[tt]<input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;1000&quot;>[/tt]

?

If so, that could be what is killing the upload. Try either removing that field from the form or setting its value to a number larger than your uploaded filesize.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Also, the content of $_FILES you posted does not include a field called &quot;error&quot;, which indicates that you are running a version of PHP between 4.1.0 (when $_FILES was introduced) and 4.2.0 (when the &quot;error&quot; element was added).

Is upgrading PHP a possibility?

Want the best answers? Ask the best questions: TANSTAAFL!!
 
- i tried changing the value of the hidden field to huge number and that made no difference.

- once the site goes live it'll be using PHP v4.3.4 (provided by my hosting people).

for development it may be tricky to upgrade. the current PHP version on my local machine is whatever came with PHPTriad.

is there an easy way to upgrade?
 
I'm out of ideas. I used the form you posted earlier in this thread, and because my test file was larger than 1000 bytes, the upload did not take place, and $_FILES['error'] contained an error code that I was able to look up here: . But I'm running 4.3.4 on a LAMP machine.

I've installed PHP on Win32 with both IIS and Apache before, using the installer provided at I've never had problems.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
thanks anyway, sleipnir214.

i'm gonna play around with it a bit more and then i might try upgrading my PHP.

i'll let you know if i find the problem.

thanks again,

ss...
 
i'm back again, and this time my pages work!

i'm not entirely sure what i did, but it just started working. i suspect it was to do with the parameters i changed in php.ini not taking effect properly.

anyway, it works!

the one other thing i needed to do was deal with the temporary uploaded file properly using move_uploaded_file().

thanks for the help and all the pointers, sleipnir214

ss...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top