0

I have a form which contains a file input:

 var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startImageUpload(this);' class='imageuploadform' ><label>" + 
    "Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><label class='imagelbl'>" + 
    "<input type='submit' name='submitImageBtn' class='sbtn' value='Upload' /></label>" + +
    "</p> <iframe class='upload_target' name='upload_target' src='#' style='wclassth:0;height:0;border:0px;solclass #fff;'></iframe></form>");

This links to a php script where it uploads the file:

<?php

   $destination_path = str_replace("//", "/", $_SERVER['DOCUMENT_ROOT']."/")."ImageFiles";

   $result = 0;

   $target_path = $destination_path . basename( $_FILES['fileImage']['name']);

   if(move_uploaded_file($_FILES['fileImage']['tmp_name'], $target_path)) {
      $result = 1;
   }

   sleep(1);
?>

<script language="javascript" type="text/javascript">window.top.window.stopImageUpload(<?php echo $result; ?>);</script>   

Problem is that in the php script it is stating I am having an undefined index wherever it states 'fileImage' in the php script. But I don't know why it is saying I am having undefined index when I have mentioned in the name attribute in the form 'fileImage'. Why is it stating I am having an undefined index for 'fileImage' in the php script?

Here is a link to an application where you can append rows which include file inputs, you can test this and see for yourself if you wish but at moment it keeps saying there is an error during uploading. application

user1324106
  • 171
  • 1
  • 3
  • 14
  • 1
    try a print_r($_FILES) to see what the array really looks like. – h00ligan Apr 11 '12 at 12:15
  • @h00ligan I will do this and get back to you – user1324106 Apr 11 '12 at 12:18
  • When I do the print_r($_FILES), it is coming with a blank array which is strange, even though when i have uploaded a file. Output: `Array ( )` – user1324106 Apr 11 '12 at 12:21
  • BTW your iframe style is messed up from doing a find on 'id' and replace with 'class'... ;) – kitti Apr 11 '12 at 12:33
  • @RyanP A you saying in the iframe code change the class to an id? – user1324106 Apr 11 '12 at 12:36
  • Please add the full, plain-text request the browser sends to your PHP script. Check if your browser has a plugin (e.g. firebug) that is able to track it or use a network sniffer. – hakre Apr 11 '12 at 12:37
  • No, in the styles... you have wclassth instead of width, solclass instead of solid... – kitti Apr 11 '12 at 12:39
  • Well i have a function where it displays a message if file is not uploaded (there is an error while uploading the file) and a message if file does upload (File uploaded is successful, I always get the unsuccesful message when uploading the file – user1324106 Apr 11 '12 at 12:41
  • @RyanP I will change this, this was the code given to me when I downloaded and extracted the uploader :) – user1324106 Apr 11 '12 at 12:42
  • Here is a link to an application where you can append rows which include file inputs, you can test this and see for yourself if you wish but at moment it keeps saying there is an error during uploading. [application] – user1324106 Apr 11 '12 at 12:54

2 Answers2

1

There can be many reasons that are the root-cause of this error, but the actual reason is, that the $_FILES array has no string index 'fileImage'.

To get a better view which indexes are available, you can do a:

var_dump(array_keys($_FILES));

See as well array_keys and var_dump.

Probably the following (in this question undocumented javascript code) is destroying something in your form or it's data:

onsubmit='startImageUpload(this);'
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Because I am uploading a file for each table row, I am using (this), if it it destroying something then what are the possibilities for me to fix this? Like I said I do need the (this) in the onsubmit line? Do you want me to include the javascript function which performs this submit? – user1324106 Apr 11 '12 at 12:33
  • `this` can be perfectly alright. I was just assuming because it's not clear from your question, this might be totally far off. As you've commented under your question, I suggest you now start with analyzing the network traffic between your browser and server because it's not clear what happens with your "upload" request (obviously it's no upload request any longer, so you need to provide more information so you can find out more). – hakre Apr 11 '12 at 12:37
  • Here is a link to an application where you can append rows which include file inputs, you can test this and see for yourself if you wish but at moment it keeps saying there is an error during uploading. [application](http://helios.hud.ac.uk/u0867587/Mobile_app/QandATable2.php) – user1324106 Apr 11 '12 at 12:53
  • @user1324106: If you want me to test it that way, you need to contract me. – hakre Apr 11 '12 at 12:59
  • Do you know any servers I could use just to test me application to see if it can successfully upload? Because I keep getting unsuccessful uploading on my server and the read and write permissions are all set. – user1324106 Apr 11 '12 at 13:03
  • You need to debug your script first. Debug is the opposite of wildly guessing. – hakre Apr 11 '12 at 13:06
  • The only error I am getting from the debug is this: ` `. But the code was already in the file when I extracted the file from its content so I don't know why this is a problem if this was the code given when downloading the uploader – user1324106 Apr 11 '12 at 13:14
  • Found out if I go on the demo page which contains the uploader from the site, that also has the same error above but as the file uploaded was successful the error was: `` – user1324106 Apr 11 '12 at 13:18
0

I have encountered the same problem before and it was because of my ajax/javascript code. As a test, try sending your form without javascript. In that way, you will know where exactly the problem is. If the error goes away, your problem might be with javascript.

I never really got the right solution to upload files using ajax. I solved the problem by sending the form into an iframe and then collecting feedback once it is done loading.

I'd do something like this on the form tag: <form action="..." target="myframe">. create a frame with name="myframe" and onload="getResults()" This is vague, but I hope it does give you an idea.

Just be careful when you use onload because it will fire twice viz. when the page is done loading for the first time, and when the form is done submitting.

I hope I haven't completely misunderstood your point here.

Sthe
  • 2,575
  • 2
  • 31
  • 48