Jump to content

Re-populating text fields for Image Upload?


Solarpitch

Recommended Posts

Hi Guys,

 

When a user submits a form on my page and there are errors, the page returns the errors and re-populates the correct fields - So the user doesnt have to complete them again. I using the below code to do that...

 

<input style="width:200px; "  name="userfile" type="file"  id="menu_textbox" value="<?print $file_path; ?>" />

 

However, for the file upload field above it doesnt seem to work and the user must select the file they wish to upload from there computer again. Should the code for this be a little different or is there something I am missing?

Link to comment
Share on other sites

You can try this:

 

<input style="width:200px;" name="userfile" type="file" id="menu_textbox" value="<? if(isset($_POST['submit'])) echo $_POST['userfile']; ?>">

 

I tried this and it still doesnt seem to work. The userfile text field still clears when the form returns errors instead of holding the value that the user selected.

Link to comment
Share on other sites

Not the page, its massive. Heres the important parts...

 


[b]// FILE CODE[/b]

function generate_rand($l){
$c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
for($i=0; $i<$l; $i++)
{
$rand.= $c[rand()%strlen($c)];
}
return $rand;
}

$uploaddir = "user_images/";
$maxfilesize = 1048576; //1 megabyte
$filename = $_FILES['userfile']['name'];
$filesize = $_FILES['userfile']['size'];
$filetmpname = $_FILES['userfile']['tmp_name'];
$valid = array (".jpg",".gif",".png");

if ($filename) {
$error = "";
if ($filesize == 0) {
$error_file .= "The submitted file was invalid.<br />";

$execute = "no";
}
$type = strtolower(strstr($filename, '.'));
if (!in_array($type, $valid)) {
$error_file .= "The submitted file was of invalid type.<br />";

$execute = "no";
}
if ($filesize>$maxfilesize) {
$error_file .= "The submitted file was larger than a Megabyte.<br />";

$execute = "no";
}
$randnum = generate_rand(10);
$randnum .= $type;
$file_exists = true;
while ($file_exists) {
if (file_exists("$uploaddir$randnum")) {
$randnum = generate_rand(10);
$randnum .= $type;
}else{
$file_exists = false;
}
}
if ($error_file == "") {
if (move_uploaded_file($filetmpname, "$uploaddir$randnum")) {
chmod("$uploaddir$randnum", 0644);
$file_complete = "";
} 
else {
$file_complete = "Your file could not be uploaded.";

$execute = "no";
}
}else{
echo "";
}
}else{
echo "";
}


[b]// EXECUTE FORM CODE[/b]

if (($_POST['process'] == 4) & ($execute != "no"))
{
..... do insertion to database stuff
}


[b]// HTML FORM PART[/b]

<form name="myform" method="post" action="upload_ad.php">

...

<input  type="hidden" name="MAX_FILE_SIZE" value="1048576)">
<input style="width:200px; "  name="userfile" type="file"  id="menu_textbox" value="<? if(isset($_POST['process'])) 
{
echo $_POST['userfile']; 
}
?>" />







Link to comment
Share on other sites

theirs aworkaround but due to the security risks most have been coved as their classed as exploits and as any that may know, i couldn't say as they are a major secuirty risk.

 

think about it your viewing a page and any file on your HDD could be taken without you knowing.. this is why its read only

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.