Jump to content

Upload file to server


Pavlin24

Recommended Posts

Hi

I am begginer in PHP. I educate myself here since about month. I have pass through basic topics in PHP tutoral and learned lot of things. But I can not manage with file uploading. I have passed through file upload tutoral several times and understud suggested script. But when I try to do this with my files on my site it does not work. Finaly I copyed the script from tutoral in my files, but it does not work again. Can somebody help me?

I am attaching two files that I put into my site's directory.

form_upload_file.html

upload_file.php

  • Like 1
Link to comment
Share on other sites

what happens instead? have you been able to identify though logging and debugging where it breaks?

Link to comment
Share on other sites

When I choose the file to upload, and press 'submit' button, it appears window of the browser for opening the file upload_file.php.(see attachment).

And when press OK it opens empty file, and again appears window for opening file.

 

You can try this link:

 

http://www.pavlin-angelov.com/form_upload_file.php

Doc3.doc

Link to comment
Share on other sites

I haven't downloaded your code, but if you aren't adding error handling to your code and reviewing the response for the output, then you won't know why. One thing could be errors with the $_FILES upload, or another issue could be lack of permissions to copy/move the file to where you want it to go.

 

for the benefit of most of the users on this forum, please post (not attach) the relevant parts of the code that relate to handling the file upload

Link to comment
Share on other sites

I tried another upload with an image, before it wasn't an image. Either way, I don't get a download prompt. I would recommend making sure error messages are being shown though, add this to the top of your code:

 

ini_set('display_errors', 1);

error_reporting(E_ALL);

Link to comment
Share on other sites

Hallo, and thank you for yor replies.

 

OK. I added the code that justsomeguy suggested, but there is no result.

But I noticed then in 'upload' directory there is a file called "opacity.png". May be it is your file Justoneguy ?!

 

 

Here is the code:

 

<?phpini_set('display_errors', 1);error_reporting(E_ALL);$allowedExts = array("gif", "jpeg", "jpg", "png");$temp = explode(".", $_FILES["file"]["name"]);$extension = end($temp);if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/jpg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "image/x-png")|| ($_FILES["file"]["type"] == "image/png"))&& ($_FILES["file"]["size"] < 60000)&& in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } }else { echo "Invalid file"; }?>

Link to comment
Share on other sites

Hi again and thank you for attention

 

This morning I tried to upload the file with Internet explorer. Untill now I tried with Firefox.

 

It appered this message:

 

Notice: Undefined index: file in /home/pavlinan/public_html/upload_file.php on line 7Notice: Undefined index: file in /home/pavlinan/public_html/upload_file.php on line 9Notice: Undefined index: file in /home/pavlinan/public_html/upload_file.php on line 10Notice: Undefined index: file in /home/pavlinan/public_html/upload_file.php on line 11Notice: Undefined index: file in /home/pavlinan/public_html/upload_file.php on line 12Notice: Undefined index: file in /home/pavlinan/public_html/upload_file.php on line 13Notice: Undefined index: file in /home/pavlinan/public_html/upload_file.php on line 14Invalid file

 

 

What does it mean?

Link to comment
Share on other sites

It means the $_FILES array has an undefined index called file. This usually is because of something not right on the webpage that contains the form which uploads to the PHP script that processes the form.

 

In your form upload file, try giving the name attribute another value instead of "file": <input type="file" name="file" id="file"><br> I don't know if it's causing your undefined index problem, but it may be possible because of having the id and name attributes with the name value: "file". Also make sure your webpage has a doctype: <!DOCTYPE html>

Link to comment
Share on other sites

Hi. Don E, I tried this, but no result. I have replaced "file" with "pic". It gives the same mistake (see the attachment).

 

Meanwhile, I tried to upload the file with Chrome browser. But there, in the form, the browse button (for choosing the file) is not active.

This is file form.doc

Link to comment
Share on other sites

The reason you would get the

 

Notice: Undefined index: file in /home/pavlinan/public_html/upload_file.php on line 7Notice: Undefined index: file in /home/pavlinan/public_html/upload_file.php on line 9Notice: Undefined index: file in /home/pavlinan/public_html/upload_file.php on line 10

 

IS because you are running the php first, and NOT going through the html form, first, the html form provides the information to process the upload, without this information its trying to process $_FILE that is empty of data, no data = equals no index = error.

Link to comment
Share on other sites

I am not running the php first !

First I run the 'form_upload_file.html' file, where is the form data.

 

I tried again with Chrome, this time button worked, but return the same messge for error as IE.

Link to comment
Share on other sites

Then you have error in your to code that sends empty upload file data to php script, or you are not clearing the cache/history so it uses the updated files, OR your server uses .php5 extension, and is not set up to run php5 as just .php, just try simple test

<?php echo 'Hello World';?>

run it with *.php extension, and then *.php5 (if this is the version your host server uses) and see what happens.

Link to comment
Share on other sites

One reason for the error is because the name of the file upload field on the form is different than the name PHP is using. Another possible reason is because your file is larger than PHP's post_max_size setting, if that happens then both $_POST and $_FILES are empty.

Link to comment
Share on other sites

No. I checked again.And copied the code from the tutoral lesson again. The name of field in form is 'file'. And the index of $_FILES is 'file'. I am attaching again the content of two files.

Regarding size limit, I do not know how mach is the limit, but I tried with very small file (20kB)

downloadl.doc

Edited by Pavlin24
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...