views:

24

answers:

3

Hi,

Please could someone help with the following:

Using PHP I want to be able to post the details entered into a form to a csv file. This is quite straight forward on its own, however one of the fields in the form needs to upload a file, so the csv file needs to contain a link to where the file is saved.

Thanks

A: 

Files uploaded through PHP are by default destroyed after the PHP script has executed, so you will need to move the uploaded file to a pre-designated folder to save it.

You can use the function move_uploaded_file() to do this.

Whatever you give as the destination to move_uploaded_file() can then be put in to your CSV file.

Steve H
A: 

When you upload the file you will need to use the function move_uploaded_file to put the file onto the server. So just use the same argument in that function as you do in the CSV.

laurencek
A: 

OK, it sounds to me like you have a form, one of the fields is an upload- and you want to submit the form then create a CSV from the form fields (with the upload simply showing the file location) AND upload the file?

If that is the case, handle the file upload as per normal, so the form should have (eg):

<form enctype="multipart/form-data" action="csvbuilder.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input name="uploadedfile" type="file" />
//other fields
<input type="submit" value="Upload File" />
</form>

Then in the target script of the form (csvbuilder.php):

$target_path = "uploads/";

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

move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)

To then reference the file in the CSV, you should simply echo:

"http://www.domain.com/uploads/".basename( $_FILES['uploadedfile']['name']);

The best you can do is simply have the location as per above, CSVs by default dont support links (though some programs like Excel may 'interpret' links and make them clickable if you wrap them in markup)

Ergo Summary
Yes, this works like a dream - and it's so simple! Thank you so much and to everyone else who responded. Pete.
Pete_Ev
No problem, glad it helps :)
Ergo Summary