Jump to content

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?

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.