Turner
Turner

Reputation: 135

Why is my PHP upload script not working?

I am doing some simple work with uploading a file. I am ignoring error checking and exceptions at this point just to get my uploads working. I have this HTML form:

<form action='addResult.php' method='post' enctype='multipart/form-data' target='results_iFrame' onsubmit='startUpload();'>
Entry: <input type='text' id='entry' />
Stop: <input type='text' id='stop' />
Final: <input type='text' id='final' />
Chart: <input type='file' id='chart' />
<input type='submit' value='Add' /></form>

As you can see, it calls 'addResult.php' within the iFrame 'results_iFrame'. The Javascript is just for animation purposes and to tell me when things are finished. addResult.php has this code in it (along with processing the other inputs):

$upload_dir = "../img/";
$chart_loc = $upload_dir.basename($_FILES['chart']['name']);

move_uploaded_file($_FILES['chart']['tmp_name'], $chart_loc);

print_r($_FILES);

It uses the 'chart' input from the form and tries to upload it. I have the print_r() function to display some information on $_FILES, but the array is empty, thus making this fail. What could I be doing wrong?

Upvotes: 1

Views: 258

Answers (5)

Blender
Blender

Reputation: 298176

When dealing with forms, the name= is used to reference items serverside, not the id=.

Just change all those id attributes to namess, and you're good to go (hopefully).

Upvotes: 3

James Bell
James Bell

Reputation: 604

You need to give your form fields name attributes

Upvotes: 0

Capsule
Capsule

Reputation: 6159

Your inputs have no "name" attribute, only IDs.

Upvotes: 0

icc
icc

Reputation: 301

Try adding the name attribute to your input values like so:

<input type='file' id='chart' name="myfile"/>

Upvotes: 0

coreyward
coreyward

Reputation: 80041

You have to give your input elements name attributes or they don't get passed to your script. The id is used for DOM/browser/client-side use.

There might be another problem after you resolve this, but this is a good first step.

Upvotes: 1

Related Questions