Forum Moderators: coopster

Message Too Old, No Replies

embed upload form with a form

embed upload form with a form

         

drooh

9:33 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



I would like to have a normal form that asks for certain peices of information. Then I would like there to be an upload option. I would like for this upload option to appear embeded so that they can upload the file before actually sending the form. I would also like the embeded upload option to set a hidden input with the filename so that I can store that in a mysql database. something like this

<form method="POST" action="form.php">

<input type="text" name="name">
<input type="text" name="email">

<form method="post" enctype="multipart/form-data" action="upload.php">
<input name="uploadedfile" type="file">
<input type="submit" value="upload" name="submit">
</form>

<input type="hidden" value="<?php echo $filename; ?>" name="filelocation" >

<input type="submit" value="submit" name="submit">
</form>

Obviously this example will not work, I am just wondering what approach to take and what others are doing for this. I've seen some examples that use iframes and ajax.

bkeep

11:21 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



You can access your uploaded filenames by using


$path = "/path/to/your/upload/location/";
$file = $_FILES["uploadedfile"]["name"];
$tmpfile = $_FILES["uploadedfile"]["tmp_name"];

//upload and give it a random name so no conflicts occur
$rand = mt_rand(1,3000);
$save_path = $path . $rand . $file;

//move the temp file to the proper place
if (move_uploaded_file($tmpfile, $save_path))
//do stuff like enter filename to the db
}

It probably wouldn't be a bad idea to check extensions for allowed types too. This is a function i use for that you can change the file extension to whatever you want.


function checkAllowedExt($file)
{
//check file for allowed extensions returns true if wrong type
$temp = strtolower($file);
$ext_split = split("\.",$temp);
$ext = $ext_split[1];
$allowed = array('gif', 'jpg', 'jpeg', 'png');
if (!in_array($ext, $allowed))
return true;
return false;
}

I hope that gives you an idea

Regards,
Brandon