I am getting some errors trying to upload a file to an directory. These are the errors:
我试图将文件上传到目录时遇到一些错误。这些是错误:
Notice: Undefined index: sPic in C:\wamp\www\uniqueminecraftservers\upload\upload.php on line 8
Notice: Undefined index: sPic in C:\wamp\www\uniqueminecraftservers\upload\upload.php on line 13
Notice: Undefined index: sPic in C:\wamp\www\uniqueminecraftservers\upload\upload.php on line 23
Here is my PHP:
这是我的PHP:
<?php
$name = htmlspecialchars($_POST['sName']);
$ip = htmlspecialchars($_POST['sIp']);
$type = $_POST['sType'];
$port = htmlspecialchars($_POST['sPort']);
$website = htmlspecialchars($_POST['sWeb']);
$video = htmlspecialchars($_POST['sVideo']);
$pic = ($_FILES['sPic']['name']); // line 8
$desc = htmlspecialchars($_POST['sDesc']);
$target = "/uniqueminecraftservers/slist/banners/";
$target = $target . basename( $_FILES['sPic']['name']); // line 13
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("slist") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `postdata` VALUES ('$name', '$ip', '$port', '$type', '$website', '$video', '$desc')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['sPic']['tmp_name'], $target)) // line 23
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " 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 have tried evrything I can find online while searching for the past 2 hours. I CANNOT FIGURE OUT HOW TO FIX THIS.
我尝试过在搜索过去2小时的时候可以在网上找到的东西。我无法弄清楚如何修复此问题。
Note: Running on WAMP with PHP 5.4.3
注意:使用PHP 5.4.3在WAMP上运行
1 个解决方案
#1
0
Before this question gets closed, perhaps have a ponder through this, it may be of some interest.
在这个问题被关闭之前,也许有一个思考,这可能是有一些兴趣。
<?php
try{
//connect to the database using PDO
$db = new PDO("mysql:host=localhost;dbname=slist","root", "mysql_password");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
//if there is an error catch it here
} catch( PDOException $e ) {
//display the Exception
exit($e->getMessage());
}
//Setup vars we going to use
$insert = array();
$message= null;
$error = array();
$target = "/uniqueminecraftservers/slist/banners/";
//ok is request POST?
if($_SERVER['REQUEST_METHOD'] === 'POST'){
/* Quick crude validation */
//Allowed keys we are expecting from POST
$keys = array('sName','sIp','sType','sPort','sWeb','sVideo','sDesc');
//Loop the above and match with $_POST[*]
foreach($keys as $key=>$value){
if(isset($_POST[$key])){
$insert[':'.$key] = $value;
}else{
$error[$key] = "*required";
}
}
//ok $error is empty lets go further
if(empty($error)){
//Check files array for error
if(isset($_FILES['sPic']['name']) && $_FILES['sPic']['error'] == 0){
//Writes the photo to the server
if(move_uploaded_file($_FILES['sPic']['tmp_name'], $target.basename($_FILES['sPic']['name'])))
{
//Insert into sql using a prepared query, matching placeholders
$sql = 'INSERT INTO `postdata` VALUES (:sName, :sIp, :sType, :sPort, :sWeb, :sVideo, :sDesc)';
$stmt = $db->prepare($sql);
$stmt->execute($insert);
//Tells you if its all ok
$message = "The file ".htmlspecialchars(basename($_FILES['sPic']['name']))." has been uploaded, and your information has been added to the directory";
}
else {
$error['upload_error'] = "Sorry, there was a problem uploading your file.";
}
}else{
//What was the upload error
if($_FILES['sPic']['error']==1){$error['upload_error'] = 'The uploaded file exceeds the Max filesize';}
if($_FILES['sPic']['error']==2){$error['upload_error'] = 'The uploaded file exceeds the Max filesize of '.ini_get('upload_max_filesize').'MB';}
if($_FILES['sPic']['error']==3){$error['upload_error'] = 'The uploaded file was only partially uploaded.';}
if($_FILES['sPic']['error']==4){$error['upload_error'] = 'No file was uploaded.';}
if($_FILES['sPic']['error']==6){$error['upload_error'] = 'Missing a temporary folder.';}
if($_FILES['sPic']['error']==7){$error['upload_error'] = 'Failed to write file to disk.';}
if($_FILES['sPic']['error']==8){$error['upload_error'] = 'A PHP extension stopped the file upload.';}
}
}
//Final check on whats gone on
if(!empty($error)){
//ill leave you to decide what happens here
echo '<pre>'.print_r($error,true).'</pre>';
}else{
//success
echo $message;
}
}else{
//do something if not POST
}
?>
Untested...
未经测试...
#1
0
Before this question gets closed, perhaps have a ponder through this, it may be of some interest.
在这个问题被关闭之前,也许有一个思考,这可能是有一些兴趣。
<?php
try{
//connect to the database using PDO
$db = new PDO("mysql:host=localhost;dbname=slist","root", "mysql_password");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
//if there is an error catch it here
} catch( PDOException $e ) {
//display the Exception
exit($e->getMessage());
}
//Setup vars we going to use
$insert = array();
$message= null;
$error = array();
$target = "/uniqueminecraftservers/slist/banners/";
//ok is request POST?
if($_SERVER['REQUEST_METHOD'] === 'POST'){
/* Quick crude validation */
//Allowed keys we are expecting from POST
$keys = array('sName','sIp','sType','sPort','sWeb','sVideo','sDesc');
//Loop the above and match with $_POST[*]
foreach($keys as $key=>$value){
if(isset($_POST[$key])){
$insert[':'.$key] = $value;
}else{
$error[$key] = "*required";
}
}
//ok $error is empty lets go further
if(empty($error)){
//Check files array for error
if(isset($_FILES['sPic']['name']) && $_FILES['sPic']['error'] == 0){
//Writes the photo to the server
if(move_uploaded_file($_FILES['sPic']['tmp_name'], $target.basename($_FILES['sPic']['name'])))
{
//Insert into sql using a prepared query, matching placeholders
$sql = 'INSERT INTO `postdata` VALUES (:sName, :sIp, :sType, :sPort, :sWeb, :sVideo, :sDesc)';
$stmt = $db->prepare($sql);
$stmt->execute($insert);
//Tells you if its all ok
$message = "The file ".htmlspecialchars(basename($_FILES['sPic']['name']))." has been uploaded, and your information has been added to the directory";
}
else {
$error['upload_error'] = "Sorry, there was a problem uploading your file.";
}
}else{
//What was the upload error
if($_FILES['sPic']['error']==1){$error['upload_error'] = 'The uploaded file exceeds the Max filesize';}
if($_FILES['sPic']['error']==2){$error['upload_error'] = 'The uploaded file exceeds the Max filesize of '.ini_get('upload_max_filesize').'MB';}
if($_FILES['sPic']['error']==3){$error['upload_error'] = 'The uploaded file was only partially uploaded.';}
if($_FILES['sPic']['error']==4){$error['upload_error'] = 'No file was uploaded.';}
if($_FILES['sPic']['error']==6){$error['upload_error'] = 'Missing a temporary folder.';}
if($_FILES['sPic']['error']==7){$error['upload_error'] = 'Failed to write file to disk.';}
if($_FILES['sPic']['error']==8){$error['upload_error'] = 'A PHP extension stopped the file upload.';}
}
}
//Final check on whats gone on
if(!empty($error)){
//ill leave you to decide what happens here
echo '<pre>'.print_r($error,true).'</pre>';
}else{
//success
echo $message;
}
}else{
//do something if not POST
}
?>
Untested...
未经测试...