conan318 Posted August 10, 2011 Share Posted August 10, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/244405-upload-script-broken/ Share on other sites More sharing options...
Morg. Posted August 10, 2011 Share Posted August 10, 2011 Please post your errors, troubleshooting by just looking at code is slightly harder you know Quote Link to comment https://forums.phpfreaks.com/topic/244405-upload-script-broken/#findComment-1255285 Share on other sites More sharing options...
AyKay47 Posted August 10, 2011 Share Posted August 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/244405-upload-script-broken/#findComment-1255302 Share on other sites More sharing options...
conan318 Posted August 10, 2011 Author Share Posted August 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244405-upload-script-broken/#findComment-1255304 Share on other sites More sharing options...
AyKay47 Posted August 10, 2011 Share Posted August 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/244405-upload-script-broken/#findComment-1255305 Share on other sites More sharing options...
conan318 Posted August 10, 2011 Author Share Posted August 10, 2011 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."; } } Quote Link to comment https://forums.phpfreaks.com/topic/244405-upload-script-broken/#findComment-1255324 Share on other sites More sharing options...
Morg. Posted August 10, 2011 Share Posted August 10, 2011 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 -- Quote Link to comment https://forums.phpfreaks.com/topic/244405-upload-script-broken/#findComment-1255326 Share on other sites More sharing options...
AyKay47 Posted August 10, 2011 Share Posted August 10, 2011 do somewhat of the same thing.. this time using the empty function Quote Link to comment https://forums.phpfreaks.com/topic/244405-upload-script-broken/#findComment-1255329 Share on other sites More sharing options...
conan318 Posted August 10, 2011 Author Share Posted August 10, 2011 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 -- Quote Link to comment https://forums.phpfreaks.com/topic/244405-upload-script-broken/#findComment-1255340 Share on other sites More sharing options...
conan318 Posted August 10, 2011 Author Share Posted August 10, 2011 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/244405-upload-script-broken/#findComment-1255358 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.