mysql - PHP File Upload not working with $_FILES variable it is always undefined -
ok have page user uploads file looks this:
upload.php:
<form enctype="multipart/form-data" action="upload_file.php" method="post"> choose file upload: <input name="uploaded" type="file" /><br /> <input type="submit" value="upload file" /> </form>
upload_file.php:
<?php $target = "upload/"; $target = $target . basename( $_files['uploaded']['name']) ; $ok=1; $uploaded_type= $_files['uploaded']['type']; $uploaded_size= $_files['uploaded']['size']; //this our size condition if ($uploaded_size > 350000) { echo "your file large.<br>"; $ok=0; } //this our limit file type condition if ($uploaded_type =="text/php") { echo "no php files<br>"; $ok=0; } //here check $ok not set 0 error if ($ok==0) { echo "sorry file not uploaded"; } //if ok try upload else { if(move_uploaded_file($_files['uploaded']['tmp_name'], $target)) { echo "the file ". basename( $_files['uploadedfile']['name']). " has been uploaded"; } else { echo "sorry, there problem uploading file."; } } ?>
whenever test webpage returns this
notice : undefined index: uploaded in \htdocs\upload_file.php on line 3
notice : undefined variable: uploaded_size in \htdocs\upload_file.php on line 7
notice : undefined variable: uploaded_type in \htdocs\upload_file.php on line 14
notice : undefined index: uploaded in a:\xampp\htdocs\upload_file.php on line 29
can please tell me did wrong here. sorry if simple fix.
in advance!
there couple of issues here. lots of small novice programmer stuff, did cleanup. egregious issue basic formatting made things hard read. cleaned up. also, set check @ beginning of script basic check see if $_files['uploaded']
set. also, have variable called $ok
using basic true
/false
checks. adjusted actual true
/false
values.
also, line reads:
echo "the file ". basename( $_files['uploadedfile']['name']). " has been uploaded";
makes no sense since rest of script refers $_files['uploaded']
. corrected well.
now past of that, shows when basic variable dump in script this:
echo '<pre>'; print_r($_files); echo '<pre>';
that show in $_files
array make debugging easier. heck, added script basic true
/false
switch allow switch on & off when needed.
<?php if (true) { echo '<pre>'; print_r($_files); echo '<pre>'; } $ok = true; // check if there uploaded file. if (!array_key_exists('uploaded', $_files) && !empty($_files['uploaded'])) { echo "sorry, no file seems uploaded."; $ok = false; } $target = "upload/"; $target = $target . basename($_files['uploaded']['name']) ; $uploaded_type = $_files['uploaded']['type']; $uploaded_size = $_files['uploaded']['size']; // our size condition. if ($uploaded_size > 350000) { echo "your file large.<br>"; $ok = false; } //this our limit file type condition if ($uploaded_type =="text/php") { echo "no php files<br>"; $ok = false; } // here check $ok not set 0 error if ($ok) { echo "sorry file not uploaded"; } // if ok try upload else { if (move_uploaded_file($_files['uploaded']['tmp_name'], $target)) { echo "the file ". basename( $_files['uploaded']['name']). " has been uploaded"; } else { echo "sorry, there problem uploading file."; } } ?>
Comments
Post a Comment