Shubhum Posted March 7, 2014 Share Posted March 7, 2014 Am trying to upload a file and am getting an error.. Am fed up with this.. Can anyone help me out please.. Thanks in advance.. CODE: <?php include ("db.php"); function generateKEY ($length = { $generate = ""; $possible = "012346789abcdfghjkmnpqrtvwxyz"; $maxlength = strlen($possible); if ($length > $maxlength) { $length = $maxlength; } $i = 0; while ($i < $length) { $char = substr($possible, mt_rand(0, $maxlength-1), 1); if (!strstr($generate, $char)) { $generate .= $char; $i++; } } return $generate; } $key = generateKEY(); define("UPLOAD_DIR", "uploads/"); if (!empty($_FILES["myFile"])) { $myFile = $_FILES["myFile"]; if ($myFile["error"] !== UPLOAD_ERR_OK) { echo "<p>An error occurred.</p>"; exit; } // ensure a safe filename $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]); // don't overwrite an existing file $i = 0; $parts = pathinfo($name); while (file_exists(UPLOAD_DIR . $name)) { $i++; $name = $parts["filename"] . $key . "-" . $i . "." . $parts["extension"]; } // preserve file from temporary directory $success = move_uploaded_file($myFile["tmp_name"], UPLOAD_DIR . $name); if (!$success) { echo "<p>Unable to save file.</p>"; exit; } // set proper permissions on the new file chmod(UPLOAD_DIR . $name, 0644); } echo $name; ?> ERROR: Notice: Undefined variable: name in C:\xampp\htdocs\MAGAZINE.MU\masterzone\p... on line 58 line 58 = echo $name; Additional Details Apparently, the if condition "if (!empty($_FILES["myFile"]))" is not being met.. Can anyone suggest why it is not being met please? The form which is submitting the data: <form action="php/editad.php" method="post" enctype="multipart/form-data"> <tr> <td><?php echo $nom['Banner_Name'];?></td> <td class="center"><input class="input-file uniform_on" id="file" name="myFile" type="file"></td> <td class="center"><input type="textbox" name="link"></td> <td class="center"> <input type="submit" class="btn btn-primary" value="Save"> </td> </tr> </form> Quote Link to comment https://forums.phpfreaks.com/topic/286790-php-upload-file-problem/ Share on other sites More sharing options...
Ch0cu3r Posted March 7, 2014 Share Posted March 7, 2014 ERROR: Notice: Undefined variable: name in C:\xampp\htdocs\MAGAZINE.MU\masterzone\p... on line 58 Move echo $name; so it is after this line chmod(UPLOAD_DIR . $name, 0644); $name is only defined when $_FILES["myFile"] is not empty Apparently, the if condition "if (!empty($_FILES["myFile"]))" is not being met.. Can anyone suggest why it is not being met please? The form which is submitting the data: What is the output of printf('<pre>%s</pre>', print_r($_POST, true)); printf('<pre>%s</pre>', print_r($_FILES, true)); When the form us submitted? Quote Link to comment https://forums.phpfreaks.com/topic/286790-php-upload-file-problem/#findComment-1471747 Share on other sites More sharing options...
Shubhum Posted March 7, 2014 Author Share Posted March 7, 2014 (edited) Thanks for replying.. Am gettingArray([link] => http://www.google.mu)Array()I think, the form is not submitting the file. :?From what i see, everything seems good in my code.. Any idea? Edited March 7, 2014 by Shubhum Quote Link to comment https://forums.phpfreaks.com/topic/286790-php-upload-file-problem/#findComment-1471772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.