Jump to content

PHP UPLOAD FILE PROBLEM!


Shubhum

Recommended Posts

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 = 8) 

$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>

Link to comment
https://forums.phpfreaks.com/topic/286790-php-upload-file-problem/
Share on other sites

 

 

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.