Page 1 of 1

Storing URL of an uploaded file into SQL database

Posted: Wed Feb 06, 2008 3:20 am
by misterfranky
Hi all,

I'm trying to allow my users to upload a file, the URL of which is stored into my SQL database. Here is what I have so far:

<form enctype="multipart/form-data" id="oefenstof_upload" name="oefenstof_upload" method="POST" action="
<?php
echo $editFormAction;

$target_path = "_files/oefenstof/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

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

?>">

<input name="oefenstof_uploadedby" type="hidden" value="<?php echo $slusername ?>" />
<input name="oefenstof_date" type="hidden" value="<?php echo date("Y-m-d") ?>" />
<input name="oefenstof_url" type="hidden" value="<?php echo $target_path ?>" />

<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
Kies een bestand om te uploaden:
<input name="uploadedfile" type="file" /><br />
Geef dit bestand een titel:
<input name="oefenstof_title" type="text" /><br />
<input type="submit" value="Bestand uploaden" />
<input type="hidden" name="MM_insert" value="oefenstof_upload" />
</form>
</p>

The writing into the database is handled elsewhere on the page. However, I can get the correct URL, but I cannot get the filename into my database. Could you please tell me how I could do this?

Thanks,
Frank

Re: Storing URL of an uploaded file into SQL database

Posted: Wed Feb 06, 2008 3:56 am
by Kieran Huggins
It's likely the DB query portion we'll need to see - can you post the INSERT code?

Re: Storing URL of an uploaded file into SQL database

Posted: Wed Feb 06, 2008 4:00 am
by misterfranky
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "oefenstof_upload")) {
$insertSQL = sprintf("INSERT INTO oefenstof (oefenstof_date, oefenstof_uploadedby, oefenstof_title, oefenstof_url) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['oefenstof_date'], "date"),
GetSQLValueString($_POST['oefenstof_uploadedby'], "text"),
GetSQLValueString($_POST['oefenstof_title'], "text"),
GetSQLValueString($_POST['oefenstof_url'], "text"));

Does this help?

Re: Storing URL of an uploaded file into SQL database

Posted: Wed Feb 06, 2008 4:11 am
by Kieran Huggins
"title" comes from $_POST['title'] - what you're looking for (I think) is "filename" from $_FILES['uploadedfile']['name'] - which I don't see being added to your database at all.

Re: Storing URL of an uploaded file into SQL database

Posted: Wed Feb 06, 2008 4:17 am
by misterfranky
The url that is being written into the DB comes from the hidden input in the form:

<input name="oefenstof_url" type="hidden" value="<?php echo $target_path ?>" />

However, this only echoes the url of the file, not the filename itself. I'm trying to truncate the url and the filename into a variable that I can then write into the DB, but no luck so far!

Re: Storing URL of an uploaded file into SQL database

Posted: Wed Feb 06, 2008 4:21 am
by Kieran Huggins
Since the form has not yet been submitted, PHP doesn't have anything in either $_FILES or $_POST

Re: Storing URL of an uploaded file into SQL database

Posted: Wed Feb 06, 2008 4:26 am
by misterfranky
Yes, that's true, but isn't the writing into the DB handled when the form has been submitted? Shouldn't there then be a value in $_FILES or $_POST?

Do you have a suggestion as to how I should handle this?

Thanks!