...

View Full Version : $_FILES not working?



Remotive
04-27-2012, 06:19 PM
Okay, I have made my form for an upload script I am making and the name of the file input is called 'file', so I assume this is what I am supposed to put when the form is submitted, I also have my form set to enctype="multipart/form-data" so I know that isn't the problem, which I have read is normally the culprit as to why it won't work.

Anyway here is my code...



$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // strip extention
$file_ext = substr($filename, strripos($filename, '.')); // strip name
$filesize = $_FILES["file"]["size"];
$title1 = $_POST['title'];
$title = addslashes($title1);
$link = $_POST['link'];
if (($file_ext == ".png" || $file_ext == ".gif") && ($filesize < 200000)) {
$newfilename = md5($file_basename) . $file_ext;
if (file_exists("images/" . $newfilename)) {
$error = "Unexpected error, we're working on this! Try again!";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . $newfilename);
$SQL = "INSERT INTO `images` (`title`, `link`, `src`) VALUES ('$title', '$link', 'images/$newfilename')";
$result = mysql_query($SQL);


...the only error I am getting is this; "Notice: Undefined index: file in C:\xampp\htdocs\tomaldy\pages\add_image.php on line 9" and then the exact same for line 12.

I don't understand what is happening? I am not super-experienced in PHP so I understand if it's insanely obvious what's wrong.
Do I have to $_POST my 'file' at all? I'm so confused.

Any help is extremely appreciated as I'm sure you all know how much of a pain in the neck PHP can be sometimes... :P

Once again, thanks for any help!

Fou-Lu
04-27-2012, 06:26 PM
Post the code for the HTML form as well.

Remotive
04-27-2012, 06:30 PM
<form enctype="multipart/form-data" method="post" action="pages/add_image.php" name="#menu">
<input type="text" name="title"><br/><br/>
<input type="text" name="link"><br/><br/>
<li class="upload">
<div class="fakeupload">
<input type="text" name="fakeupload" id="fakeupload"/>
</div>
<input type="file" name="file" id="realupload" class="realupload" onchange="this.form.fakeupload.value = this.value;" />
</li><br/><br/>
<input type="submit" name="go" style="width: 100px;" value="Add my image!"> - <input type="reset" name="reset" style="width: 100px;" value="Reset">
</form>


Here you go :)

MarkR
04-27-2012, 06:48 PM
How big is the file you are uploading? If it's bigger than the max size accepted by the server it'll fail. Try uploading something a couple of kilobytes in size to rule that out.

Fou-Lu
04-27-2012, 06:51 PM
Remove the javascript from the value there and try again.

Remotive
04-27-2012, 06:52 PM
How big is the file you are uploading? If it's bigger than the max size accepted by the server it'll fail. Try uploading something a couple of kilobytes in size to rule that out.

I already had done it, but I tried again anyway; the error messages showed up again on the result page... :/

The file was literally so small it was a dot pretty much.
I even set it to check file types such as .GIF and .PNG (with capitals) but still got the error messages :/

Remotive
04-27-2012, 06:58 PM
Remove the javascript from the value there and try again.

The problem still occured :/
Thanks for trying though :)

Fou-Lu
04-27-2012, 07:04 PM
In processing script add this to the top:


printf('Post<pre>%s</pre>' . PHP_EOL, print_r($_POST, true));
printf('Files<pre>%s</pre>' . PHP_EOL, print_r($_FILES, true));


View and drag the results of the browser html source and post that here.

Remotive
04-27-2012, 07:10 PM
Post
Array
(
[title] => dfdff
[link] => ihihih
[fakeupload] => C:\fakepath\delete_entry_button.gif
)
Files
Array
(
)


I suppose it could be because of the fake path?
How would I go about changing that if that's the case?

Fou-Lu
04-27-2012, 07:26 PM
Your files has not been populated.
Disable your javascript completely in the browser and try again.

Remotive
04-27-2012, 07:50 PM
Could it be because I am using AJAX to submit the form?

Fou-Lu
04-27-2012, 08:12 PM
Could it be because I am using AJAX to submit the form?

Its looking that way to me. Have you disabled your javascript and run this directly from the html through to the PHP to verify?

Remotive
04-27-2012, 08:17 PM
I got more errors about moving the file and such, but it seemed to do the trick; is there any way I could make this work with the AJAX or not?
Otherwise I would have to tell my users to upload it via a different server and then paste the image url instead of uploading it themselves?

Fou-Lu
04-27-2012, 08:23 PM
I don't understand what you mean about moving from an alternate server. Files works by moving from a local machine, not from a remote one.
As for AJAX, post in the AJAX forum for that one. I can't see why it wouldn't be doable, but I don't know what needs to be changed in order to use it.

Remotive
04-27-2012, 08:39 PM
I mean they can type the image URL rather than it being hosted on my own web hosting.

Fou-Lu
04-27-2012, 08:42 PM
I mean they can type the image URL rather than it being hosted on my own web hosting.

That's not the same as a file upload, and cannot share the same field. Have the users specify one or the other (or both) and deal with them separately. $_FILES will never be populated by an address provided unless explicitly set as such. If a remote location has been provided you need to connect to said file and copy the data across. This can be as easy as using a file_get_contents, but it depends a lot on PHP configurations. Safe bet is always using sockets.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum