By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

Uploading a file and $_FILES

P: n/a
Hello,

I am a newbie to PHP, MySQL. I am trying to create a basic file upload
form. I want to get that working and then I want to integrate that
into a form that will rename the file and save it to a directory and
store the path to the file in the db, in addition to storing other text
from other fields in the form. Then I will get that path using PHP to
display the image file in a browser.

First things first, I'm having difficulty getting the basic file upload
form working.

Here is the code I am using:

<form enctype="multipart/form-data" action="<?php print
$_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
Select a file: <input name="upfile" type="file">
<input type="submit" value="Upload">
</form>

<?php
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
?>

When I load the page the following text is always displayed on the
page:
"File upload failedArray ( )"

When I upload a file I recieve the following text on the page:
"File upload was successfulArray ( [upfile] =Array ( [name] =>
testing.php [type] =application/octet-stream [tmp_name] =>
/tmp/phpVtNHIr [error] =0 [size] =574 ) )"

I think my issue is with the $_FILES. Do I have to change the name and
tmp_name values? The documentation is sort of vague to me, but then
again I am pretty new to this. I know there are security issues and
there is a lot more that needs to be done, but I need to start with the
basics here.

Any input or advice that anyone can give would be greatly appreciated.
Also, if there are any online references that you could provide would
be great. Thanks in advance!

Aug 11 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
I am guessing you have already checked the php.net website for
assistance. http://us3.php.net/features.file-upload. The text "File
upload failedArray ( )" will always show cause it is not enclosed
within any of your if statements.

In regards to the file upload do a search for the test file if you have
access to the server. The problem, i think I had this too when I first
started, is the location you are moving the file to.

Hope this helps.
mp*****@gmail.com wrote:
Hello,

I am a newbie to PHP, MySQL. I am trying to create a basic file upload
form. I want to get that working and then I want to integrate that
into a form that will rename the file and save it to a directory and
store the path to the file in the db, in addition to storing other text
from other fields in the form. Then I will get that path using PHP to
display the image file in a browser.

First things first, I'm having difficulty getting the basic file upload
form working.

Here is the code I am using:

<form enctype="multipart/form-data" action="<?php print
$_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
Select a file: <input name="upfile" type="file">
<input type="submit" value="Upload">
</form>

<?php
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
?>

When I load the page the following text is always displayed on the
page:
"File upload failedArray ( )"

When I upload a file I recieve the following text on the page:
"File upload was successfulArray ( [upfile] =Array ( [name] =>
testing.php [type] =application/octet-stream [tmp_name] =>
/tmp/phpVtNHIr [error] =0 [size] =574 ) )"

I think my issue is with the $_FILES. Do I have to change the name and
tmp_name values? The documentation is sort of vague to me, but then
again I am pretty new to this. I know there are security issues and
there is a lot more that needs to be done, but I need to start with the
basics here.

Any input or advice that anyone can give would be greatly appreciated.
Also, if there are any online references that you could provide would
be great. Thanks in advance!
Aug 11 '06 #2

P: n/a

mrsticks1982 wrote:
I am guessing you have already checked the php.net website for
assistance. http://us3.php.net/features.file-upload. The text "File
upload failedArray ( )" will always show cause it is not enclosed
within any of your if statements.

In regards to the file upload do a search for the test file if you have
access to the server. The problem, i think I had this too when I first
started, is the location you are moving the file to.

Hope this helps.
Thanks for the response, but I'm not sure that I understand what you
are saying. What don't I have included in my if statements? Thanks
again!

Aug 11 '06 #3

P: n/a
mp*****@gmail.com wrote:
Hello,

I am a newbie to PHP, MySQL. I am trying to create a basic file upload
form. I want to get that working and then I want to integrate that
into a form that will rename the file and save it to a directory and
store the path to the file in the db, in addition to storing other text
from other fields in the form. Then I will get that path using PHP to
display the image file in a browser.

First things first, I'm having difficulty getting the basic file upload
form working.

Here is the code I am using:

<form enctype="multipart/form-data" action="<?php print
$_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
Select a file: <input name="upfile" type="file">
<input type="submit" value="Upload">
</form>

<?php
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
?>

When I load the page the following text is always displayed on the
page:
"File upload failedArray ( )"

When I upload a file I recieve the following text on the page:
"File upload was successfulArray ( [upfile] =Array ( [name] =>
testing.php [type] =application/octet-stream [tmp_name] =>
/tmp/phpVtNHIr [error] =0 [size] =574 ) )"

I think my issue is with the $_FILES. Do I have to change the name and
tmp_name values? The documentation is sort of vague to me, but then
again I am pretty new to this. I know there are security issues and
there is a lot more that needs to be done, but I need to start with the
basics here.

Any input or advice that anyone can give would be greatly appreciated.
Also, if there are any online references that you could provide would
be great. Thanks in advance!
Yes, the message will always be displayed because when you first load
the page you don't have anything in your $_FILES array. That only comes
after you've entered a filename and submitted your form.

Remember - PHP is SERVER SIDE. By the time your browser gets the page,
ALL of the PHP code has been executed.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 11 '06 #4

P: n/a
mp*****@gmail.com wrote:
Hello,

I am a newbie to PHP, MySQL. I am trying to create a basic file upload
form. I want to get that working and then I want to integrate that
into a form that will rename the file and save it to a directory and
store the path to the file in the db, in addition to storing other text
from other fields in the form. Then I will get that path using PHP to
display the image file in a browser.

First things first, I'm having difficulty getting the basic file upload
form working.

Here is the code I am using:

<form enctype="multipart/form-data" action="<?php print
$_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
Select a file: <input name="upfile" type="file">
<input type="submit" value="Upload">
</form>

<?php
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
?>

When I load the page the following text is always displayed on the
page:
"File upload failedArray ( )"

When I upload a file I recieve the following text on the page:
"File upload was successfulArray ( [upfile] =Array ( [name] =>
testing.php [type] =application/octet-stream [tmp_name] =>
/tmp/phpVtNHIr [error] =0 [size] =574 ) )"

I think my issue is with the $_FILES. Do I have to change the name and
tmp_name values? The documentation is sort of vague to me, but then
again I am pretty new to this. I know there are security issues and
there is a lot more that needs to be done, but I need to start with the
basics here.

Any input or advice that anyone can give would be greatly appreciated.
Also, if there are any online references that you could provide would
be great. Thanks in advance!
Jerry told you the reason, and here is (sort of) solution. Execute php
code only when page was POST requested (meaning that the form was submitted)
to do so simply wrap your php code in one more if...
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
}
?>

That should work... :)
--

B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
Assasination
Aug 11 '06 #5

P: n/a

B.r.K.o.N.j.A wrote:
mp*****@gmail.com wrote:
Hello,
>
I am a newbie to PHP, MySQL. I am trying to create a basic file upload
form. I want to get that working and then I want to integrate that
into a form that will rename the file and save it to a directory and
store the path to the file in the db, in addition to storing other text
from other fields in the form. Then I will get that path using PHP to
display the image file in a browser.
>
First things first, I'm having difficulty getting the basic file upload
form working.
>
Here is the code I am using:
>
<form enctype="multipart/form-data" action="<?php print
$_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
Select a file: <input name="upfile" type="file">
<input type="submit" value="Upload">
</form>
>
<?php
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
?>
>
When I load the page the following text is always displayed on the
page:
"File upload failedArray ( )"
>
When I upload a file I recieve the following text on the page:
"File upload was successfulArray ( [upfile] =Array ( [name] =>
testing.php [type] =application/octet-stream [tmp_name] =>
/tmp/phpVtNHIr [error] =0 [size] =574 ) )"
>
I think my issue is with the $_FILES. Do I have to change the name and
tmp_name values? The documentation is sort of vague to me, but then
again I am pretty new to this. I know there are security issues and
there is a lot more that needs to be done, but I need to start with the
basics here.
>
Any input or advice that anyone can give would be greatly appreciated.
Also, if there are any online references that you could provide would
be great. Thanks in advance!
>

Jerry told you the reason, and here is (sort of) solution. Execute php
code only when page was POST requested (meaning that the form was submitted)
to do so simply wrap your php code in one more if...
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
}
?>

That should work... :)
--

B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
Assasination
Thank you for the response. It's still not working. I checked the
path and I checked the file permissions and everything looks right.

I get the following from print_r: Array ( [upfile] =Array ( [name] =>
Jets.jpg [type] =image/jpeg [tmp_name] =/tmp/phpu6J23o [error] =0
[size] =27241 ) )

I think those results look to be normal, but I'm not 100% sure. Does
anyone have any thoughts on this?

Thanks!

Aug 12 '06 #6

P: n/a
mpar612 wrote:
B.r.K.o.N.j.A wrote:
>>mp*****@gmail.com wrote:
Hello,

I am a newbie to PHP, MySQL. I am trying to create a basic file upload
form. I want to get that working and then I want to integrate that
into a form that will rename the file and save it to a directory and
store the path to the file in the db, in addition to storing other text
from other fields in the form. Then I will get that path using PHP to
display the image file in a browser.

First things first, I'm having difficulty getting the basic file upload
form working.

Here is the code I am using:

<form enctype="multipart/form-data" action="<?php print
$_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
Select a file: <input name="upfile" type="file">
<input type="submit" value="Upload">
</form>

<?php
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
?>

When I load the page the following text is always displayed on the
page:
"File upload failedArray ( )"

When I upload a file I recieve the following text on the page:
"File upload was successfulArray ( [upfile] =Array ( [name] =>
testing.php [type] =application/octet-stream [tmp_name] =>
/tmp/phpVtNHIr [error] =0 [size] =574 ) )"

I think my issue is with the $_FILES. Do I have to change the name and
tmp_name values? The documentation is sort of vague to me, but then
again I am pretty new to this. I know there are security issues and
there is a lot more that needs to be done, but I need to start with the
basics here.

Any input or advice that anyone can give would be greatly appreciated.
Also, if there are any online references that you could provide would
be great. Thanks in advance!

Jerry told you the reason, and here is (sort of) solution. Execute php
code only when page was POST requested (meaning that the form was submitted)
to do so simply wrap your php code in one more if...
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
}
?>

That should work... :)
--

B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
Assasination


Thank you for the response. It's still not working. I checked the
path and I checked the file permissions and everything looks right.

I get the following from print_r: Array ( [upfile] =Array ( [name] =>
Jets.jpg [type] =image/jpeg [tmp_name] =/tmp/phpu6J23o [error] =0
[size] =27241 ) )

I think those results look to be normal, but I'm not 100% sure. Does
anyone have any thoughts on this?

Thanks!
What message are you getting now? And what's the response from
move_uploaded_file?

You're trying to put the file in the 'uploads' directory, which would be
relative to this script. Does it exist, and does the Apache user have
write access to this directory? Does the file exist?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 12 '06 #7

P: n/a

Jerry Stuckle wrote:
mpar612 wrote:
B.r.K.o.N.j.A wrote:
>mp*****@gmail.com wrote:
Hello,

I am a newbie to PHP, MySQL. I am trying to create a basic file upload
form. I want to get that working and then I want to integrate that
into a form that will rename the file and save it to a directory and
store the path to the file in the db, in addition to storing other text
from other fields in the form. Then I will get that path using PHP to
display the image file in a browser.

First things first, I'm having difficulty getting the basic file upload
form working.

Here is the code I am using:

<form enctype="multipart/form-data" action="<?php print
$_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
Select a file: <input name="upfile" type="file">
<input type="submit" value="Upload">
</form>

<?php
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
?>

When I load the page the following text is always displayed on the
page:
"File upload failedArray ( )"

When I upload a file I recieve the following text on the page:
"File upload was successfulArray ( [upfile] =Array ( [name] =>
testing.php [type] =application/octet-stream [tmp_name] =>
/tmp/phpVtNHIr [error] =0 [size] =574 ) )"

I think my issue is with the $_FILES. Do I have to change the name and
tmp_name values? The documentation is sort of vague to me, but then
again I am pretty new to this. I know there are security issues and
there is a lot more that needs to be done, but I need to start with the
basics here.

Any input or advice that anyone can give would be greatly appreciated.
Also, if there are any online references that you could provide would
be great. Thanks in advance!


Jerry told you the reason, and here is (sort of) solution. Execute php
code only when page was POST requested (meaning that the form was submitted)
to do so simply wrap your php code in one more if...
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
}
?>

That should work... :)
--

B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
Assasination

Thank you for the response. It's still not working. I checked the
path and I checked the file permissions and everything looks right.

I get the following from print_r: Array ( [upfile] =Array ( [name] =>
Jets.jpg [type] =image/jpeg [tmp_name] =/tmp/phpu6J23o [error] =0
[size] =27241 ) )

I think those results look to be normal, but I'm not 100% sure. Does
anyone have any thoughts on this?

Thanks!

What message are you getting now? And what's the response from
move_uploaded_file?

You're trying to put the file in the 'uploads' directory, which would be
relative to this script. Does it exist, and does the Apache user have
write access to this directory? Does the file exist?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Thanks for your response. I think I just solved my own problem. I
need to ensure that the temporary directory is titled "tmp_name". Is
there a particular reason why the directory has to be called
"tmp_name"? I tried changing it to just "tmp" and the script would not
work.

Thank you so much for all of your help.

Aug 13 '06 #8

P: n/a
mpar612 wrote:
Jerry Stuckle wrote:
>>mpar612 wrote:
>>>B.r.K.o.N.j.A wrote:
mp*****@gmail.com wrote:

>Hello,
>
>I am a newbie to PHP, MySQL. I am trying to create a basic file upload
>form. I want to get that working and then I want to integrate that
>into a form that will rename the file and save it to a directory and
>store the path to the file in the db, in addition to storing other text
>from other fields in the form. Then I will get that path using PHP to
>display the image file in a browser.
>
>First things first, I'm having difficulty getting the basic file upload
>form working.
>
>Here is the code I am using:
>
><form enctype="multipart/form-data" action="<?php print
>$_SERVER['PHP_SELF']; ?>" method="post">
><input type="hidden" name="MAX_FILE_SIZE" value="50000">
>Select a file: <input name="upfile" type="file">
><input type="submit" value="Upload">
></form>
>
><?php
>$uploaddir = "uploads/";
>$uploadfile = $uploaddir . $_FILES['upfile']['name'];
>if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
> print("File upload was successful");
> } else {
> print("File upload failed");
> }
> print_r($_FILES);
>?>
>
>When I load the page the following text is always displayed on the
>page:
>"File upload failedArray ( )"
>
>When I upload a file I recieve the following text on the page:
>"File upload was successfulArray ( [upfile] =Array ( [name] =>
>testing.php [type] =application/octet-stream [tmp_name] =>
>/tmp/phpVtNHIr [error] =0 [size] =574 ) )"
>
>I think my issue is with the $_FILES. Do I have to change the name and
>tmp_name values? The documentation is sort of vague to me, but then
>again I am pretty new to this. I know there are security issues and
>there is a lot more that needs to be done, but I need to start with the
>basics here.
>
>Any input or advice that anyone can give would be greatly appreciated.
>Also, if there are any online references that you could provide would
>be great. Thanks in advance!
>

Jerry told you the reason, and here is (sort of) solution. Execute php
code only when page was POST requested (meaning that the form was submitted)
to do so simply wrap your php code in one more if...
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
}
?>

That should work... :)
--

B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
Assasination
Thank you for the response. It's still not working. I checked the
path and I checked the file permissions and everything looks right.

I get the following from print_r: Array ( [upfile] =Array ( [name] =>
Jets.jpg [type] =image/jpeg [tmp_name] =/tmp/phpu6J23o [error] =0
[size] =27241 ) )

I think those results look to be normal, but I'm not 100% sure. Does
anyone have any thoughts on this?

Thanks!

What message are you getting now? And what's the response from
move_uploaded_file?

You're trying to put the file in the 'uploads' directory, which would be
relative to this script. Does it exist, and does the Apache user have
write access to this directory? Does the file exist?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


Thanks for your response. I think I just solved my own problem. I
need to ensure that the temporary directory is titled "tmp_name". Is
there a particular reason why the directory has to be called
"tmp_name"? I tried changing it to just "tmp" and the script would not
work.

Thank you so much for all of your help.
The directory itself can be any directory you have write access to. The
array index, however, must be 'tmp_name' - it's defined that way in PHP.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 13 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.