Wednesday, 15 August 2012

php - hide input field if upload succeed -



php - hide input field if upload succeed -

i hide input field if upload succeed. tried hide putting $sub =1; , if($sub==0) below hides input whenever submit pressed not when upload succeed. ideas?

<?php function uploadfile ($file_field = null, $check_image = false, $random_name = false) { //config section //set file upload path $path = 'productpic/'; //with trailing slash //set max file size in bytes $max_size = 2097152; //set default file extension whitelist $whitelist_ext = array('jpg','png','gif'); //set default file type whitelist $whitelist_type = array('image/jpeg', 'image/png','image/gif'); //the validation // create array hold output $out = array('error'=>null); if (!$file_field) { $out['error'][] = "please specify valid form field name"; } if (!$path) { $out['error'][] = "please specify valid upload path"; } if (count($out['error'])>0) { homecoming $out; } //make sure there file if((!empty($_files[$file_field])) && ($_files[$file_field]['error'] == 0)) { // filename $file_info = pathinfo($_files[$file_field]['name']); $name = $file_info['filename']; $ext = $file_info['extension']; //check file has right extension if (!in_array($ext, $whitelist_ext)) { $out['error'][] = "invalid file extension"; } //check file of right type if (!in_array($_files[$file_field]["type"], $whitelist_type)) { $out['error'][] = "invalid file type"; } //check file not big if ($_files[$file_field]["size"] > $max_size) { $out['error'][] = "we sorry, image must less 2mb"; } //if $check image set true if ($check_image) { if (!getimagesize($_files[$file_field]['tmp_name'])) { $out['error'][] = "the file trying upload not image, take images"; } } //create total filename including path if ($random_name) { // generate random filename $tmp = str_replace(array('.',' '), array('',''), microtime()); if (!$tmp || $tmp == '') { $out['error'][] = "file must have name"; } $newname = $tmp.'.'.$ext; } else { $newname = $name.'.'.$ext; } //check if file exists on server if (file_exists($path.$newname)) { $out['error'][] = "the image trying upload exists, please upload once"; } if (count($out['error'])>0) { //the file has not correctly validated homecoming $out; } if (move_uploaded_file($_files[$file_field]['tmp_name'], $path.$newname)) { //success $out['filepath'] = $path; $out['filename'] = $newname; homecoming $out; } else { $out['error'][] = "server error!"; } } else { $out['error'][] = "please select photo"; homecoming $out; } } ?> <?php if (isset($_post['submit'])) { $sub=1; $file = uploadfile('file', true, false); if (is_array($file['error'])) { $message = ''; foreach ($file['error'] $msg) { $message .= '<p>'.$msg.'</p>'; } } else { $message = "file uploaded successfully"; } echo $message; } ?> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> <?php ini_set( "display_errors", 0); if($sub==0) { ?> <input name="file" type="file" size="20" /> <input name="submit" type="submit" value="upload" /> <?php } ?> </form>

try this

if (is_array($file['error'])) { $message = ''; foreach ($file['error'] $msg) { $message .= '<p>'.$msg.'</p>'; } } else { $message = "file uploaded successfully"; $sub=1; // maintain here sub=1 }

the $sub=1 executing when submit pressed since given in if (isset($_post['submit'])) means if submit set.it should given when upload completes.

php

No comments:

Post a Comment