Jump to content

Setting file upload directory


fry2010

Recommended Posts

Im not sure if this is really the right place for it. Basically im trying to specify the actuall path of an uploaded file.

 

So basically the user is filling out a form, and they upload an image. Lets say the form has errors in it. At the moment I have it so that it will tell them what errors were made when they submit it. It will show them the form again but with the correct values already stored in the fields so they dont end up typing it out again.

 

I would like to get this to work for the file upload field aswell. I have tried the following:

 

<?php
$img_file = $_FILES['img'];

// DO FILE CHECKS ETC....

// THERE WAS AN ERROR IN THE FORM, BUT THE FILE UPLOAD WAS OK...

if($error)
{
  if(!$img_file_error)
  {
     ?>
  <input type="file" name="img" <?php echo 'value="'.$img_file['tmp_name'].'"; ?>>

    <?php
  }
?>

 

So what I have tried to do is the same as you would do on a normal text field, by setting the value="" to what I want inside that field. But this doesnt work with type="file" fields.

 

Is there another attribute that I can specify the directory that the user submits?

 

I no it can be done with js, but this form is for people who dont have js enabled. Anyone have any ideas?

Link to comment
Share on other sites

yeah I tried that too, unfortunatly its the same thing.

 

I have echoed out the values that $_FILES['img']['name'] and they have the correct text, its just not putting it inside the field.

 

Should it be doing so then?

Link to comment
Share on other sites

oh yeah that happened aswell, but I sorted that out. Dont no how you spotted it though without having an error thrown at you lol. Its there just because I hand typed that just now, so didnt check with that.

Link to comment
Share on other sites

No no.. that happened ages ago, its stil not happening.

 

Infact I have checked the source code and it shows that its in the value="" field as:

 

value="C:\WINDOWS\Temp\php1E1.tmp"

 

Thats for ['tmp_name']

 

and for ['name'] it is:

 

value="Sample.jpg"

 

and that is in the actual HTML source code, so I imagine you cant use value="" to specify what you want inside the file type...

 

Link to comment
Share on other sites

You mean that you cant use a value="" field to specify what you want inside a type="file"? or that my code had a delimeter missing?

 

it should have been like this, i just made a typo ignore that:

 

<input type="file" name="img" <?php echo 'value="'.$img_file['tmp_name'].'"'; ?>>

Link to comment
Share on other sites

No not that either, I missed that out aswell lol, iv been trying to do valid html since reading another article about it.

 

Here is the exact output from my browsers source code:

 

<?php
  <div>
    <label for="main_img" >Main Image: (This will be visible as part of your listing, outside your main page.
                            We recommend an image size of around 800px on the longest side.)</label>
    <input type="file" name="main_img" value="C:\WINDOWS\Temp\php1E8.tmp" />

?>

 

And in my actuall form field for the "main_img" field it is blank, no text at all...

 

I really appreciate ur guys help in this.

Link to comment
Share on other sites

oh rite, that explains it then.

However, I have come accross a site that actually keeps the value of the file upload inside it when you do a page refresh. I have switched javascript off and it still works?? I wonder how he did it?

 

http://www.elated.com/articles/html-forms--hidden-fields-password-fields-and-file-upload-fields/

 

At the bottom there is a file upload field, and putting a file in there and then refreshing with js turned off it still remains there..

Link to comment
Share on other sites

That's because it is the browser keeping the value in the field on a page refresh.

 

The only way to put a value in a file field is by using the browser's file selection dialog box. HTML and javascript cannot put a value in.

Link to comment
Share on other sites

Oh right. I thought I read somewhere about javascript being able to do so... Probably not exactly what I was looking for though I just skimmed it because it was js.

I suppose the only real option I have is if the file validates, just place it underneath the file input box to show they already have it loaded. It will save them the hassle of having to search their files again.

Thanks for the help ken, PFMa...., and maple.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.