Jump to content

Recommended Posts

background.php 2 undefined errors i use this same script else where on my site and have

<?php





mysql_connect("localhost", "admin", "admin")or die("cannot connect"); 
mysql_select_db("main")or die("cannot select DB");
$img=$_POST['img'];// Notice: Undefined index: img in C:\wamp\www\Kurri\background.php on line 9



/* explode breaks down the string*/
$ext_a = explode(".", $_FILES['img']['name']);
$ext = $ext_a[1];
/* hashes the file name with the date and time to genrate a uniue file name*/
$img=md5($_FILES['img']['name'] . date("m.d.y H:m:s")) . "." . $ext;
$target = "img/"; 
$target = $target .basename($img); 


           
        


/* Now we will write a query to insert user details into database */
$insert_user=mysql_query("INSERT INTO main.background (img) VALUES '$img')") ;


//Writes the photo to the server 
if(move_uploaded_file($_FILES['img']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['name']['img']). " has been uploaded, and your information has been added to the directory"; //Notice: Undefined index: name in C:\wamp\www\Kurri\background.php on line 35
} 
else {

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 

mb-admin.html

<div class="appearance">
  <form method="post" action="background.php" enctype="multipart/form-data">
  <label for="img">Change Background Image</label>
      <input type="file" name="img" /><br />
      <input type="submit" value="submit" />
      </form>
      

</div>

Link to comment
https://forums.phpfreaks.com/topic/244405-upload-script-broken/
Share on other sites

i am assuming that the undefined errors are undefined indices..this is because you do not check to see if your form has been submitted using the isset funtion..

 

if(isset($_POST['submit'])){ //this is assmuming that you name your submit type input "submit" 
   //form validation code
}

 

if you are receiving different errors than this...let us know

sorry i posted the error in the code  // Notice: Undefined index: img in C:\wamp\www\Kurri\background.php on line 9 and Undefined index: name in C:\wamp\www\Kurri\background.php on line 36

 

then gives the message file has been upload but the file has not been uploaded.

 

 

sorry i posted the error in the code  // Notice: Undefined index: img in C:\wamp\www\Kurri\background.php on line 9 and Undefined index: name in C:\wamp\www\Kurri\background.php on line 36

 

then gives the message file has been upload but the file has not been uploaded.

refer to my post then

i just changed the code to this and named my submit button submit and now getting same error.

Notice: Undefined index: name in C:\wamp\www\Kurri\background.php on line 37

 

<?php





mysql_connect("localhost", "admin", "admin")or die("cannot connect"); 
mysql_select_db("main")or die("cannot select DB");




if(isset($_POST['submit'])){
$img=$_POST['img'];
/* explode breaks down the string*/
$ext_a = explode(".", $_FILES['img']['name']);
$ext = $ext_a[1];
/* hashes the file name with the date and time to genrate a uniue file name*/
$img=md5($_FILES['img']['name'] . date("m.d.y H:m:s")) . "." . $ext;
$target = "img/"; 
$target = $target .basename($img); 


           
        


/* Now we will write a query to insert user details into database */
$insert_user=mysql_query("INSERT INTO main.background (img) VALUES '$img')") ;


//Writes the photo to the server 
if(move_uploaded_file($_FILES['img']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['name']['img']). " has been uploaded, and your information has been added to the directory";
} 
else {

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} }

I know what error means the only variable i have set is $img but i am unclear where the name is variable is meant to be coming from. and the script i have working on my other page does not set a name variable. i was amusing the name was coming form the $_files global variable.

Seriously ...

UNDEFINED means that variable is not SET.

YOUR code checks for files[name] AND for files[name] ... I have quite a doubt both would be set --

<?php
$ext_a = explode(".", $_FILES['uploadedfile']['name']);
$ext = $ext_a[1];
/* hashes the file name with the date and time to genrate a uniue file name*/
$img=md5($_FILES['uploadedfile']['name'] . date("m.d.y H:m:s")) . "." . $ext;
$target_path = "back/";


$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
$insert_user=mysql_query("INSERT INTO main.back (img) VALUES ('$img')") ;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

 

all working now i just got to put in a check to make sure its a img file being uploaded

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.