我创建了一个脚本,以便一个人将文本信息和图片上传到数据库。图片托管在名为“images”的网站的子文件夹中。
我面临的问题是该文件夹正在上传到该文件夹,但文件名未记录在数据库中。
我知道这不是连接问题,因为文件字段被记录在数据库中,而文件名被留作空白。
我认为这可能是我的checkUpload()方法的一个问题。
这是我的整体剧本
<?php
printForm();
//when "submit" tie together values and variables
if($_POST['submit']=="Submit"){
$email = cleanData($_POST['email']);
$first = cleanData($_POST['first']);
$last = cleanData($_POST['last']);
$status = cleanData($_POST['status']);
//$image = cleanData($_POST['image']);
//echo "Data cleaned";
addData($email, $first, $last, $status);
}
else{
//printForm();
}
function checkUpload(){
//check for uploaded files
if(isset($_FILES['upload'])){ //upload refers to form element "upload"
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/GIF');
if(in_array($_FILES['upload']['type'], $allowed)){//if upload if in the allowed array 'file types'
echo "uploading files...";
//move the file over
if(move_uploaded_file($_FILES['upload']['tmp_name'], "images/{$_FILES['upload']['name']}")){
//moveuf method moves to tmp folder then moves to final location
echo "<p>The file has been uploaded 'dude'</p>";
$image="{$_FILES['upload']['name']}";
}//end of moving DAT IMG :3
else{
echo '<p>Please upload a JPEG, GIF or PNG image.<strong>';
if($FILES['upload']['error'] > 0){
echo '<b>The file could not be updated because</b>';
switch($_FILES['upload']['error']){//standard error casee
case 1:
echo 'The file exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
echo 'The file exceeds the MAX_FILE_SIZE in the HTML form.';
break;
case 3:
echo 'The file only partially uploaded';
break;
case 4:
echo 'The no file was uploaded';
break;
case 6:
echo 'No temporary folder was available';
break;
case 7:
echo 'Unable to write to the disk';
break;
case 8:
echo 'File upload stopped';
break;
default:
echo 'A system error occurred';
break;
}
echo '</strong></p>';
}
//if the file is still in the temporary folder "unlink" which is basically just deleting the SOB
if(file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])){
echo 'File exists! :D';
unlink ($_FILES['upload']['tmp_name']);
}
return $image;
//gives back image where the function was called
}
}
}
}
//cleans information
function cleanData($data){
if(!$status){ //everything except for status take out spaces
$data = trim($data);
}
$data = stripslashes($data);//no slashes
$data = htmlspecialchars($data);//no special characters
$data = strip_tags($data);//no html tags
return $data;
}
//inserts data into db
function addData($email, $first, $last, $status){
//echo "Ready to add data";
include("dbinfo.php");//access db
$image = checkUpload();
$sql = "INSERT INTO contacts VALUES(null, '$email', '$first', '$last', '$status', '$image')";
//null because of ID aka primary key automatically incremented:3
$result = mysql_query($sql) or die(mysql_error());
//takes sql arugment for query OR if it can't you get a BUMMER DUDE
echo <<<HERE
<b>The following has been added:</b>
<ul>
<li>E-mail: $email</li>
<li>First: $first</li>
<li>Last: $last</li>
<li>Status: $status</li>
<li>Image File:<br/> <img src="images/$image" /></li>
</ul>
HERE;
}
function printForm(){
$pageTitle ="Add a Contact";
include("header.php");
echo <<<EOD
<b>Add a Contact</b>
<form method = "POST" enctype="multipart/form-data">
<div>
<label for="email">Email*:</label>
<input type="text" name="email" id="email" required="required">
</div>
<div>
<label for="first">First Name*:</label>
<input type="text" name="first" id="first" required="required">
</div>
<div>
<label for="last">Last Name*:</label>
<input type="text" name="last" id="last" required="required">
</div>
<div>
<label for="status">Status*:</label>
<input type="text" name="status" id="status" required="required">
</div>
<div>
<label for="image">Image*:</label>
<input type="file" name="upload" size="30" id="upload" required="required"><br/>
<small>Must be less than 512kb. Only JPG, GIF, and PNG files</small>
</div>
<div id="mySubmit">
<input type="submit" name="submit" value="Submit">
</div>
</form>
EOD;
}
include("footer.php");
?>
另外 - 作为旁注,在我的上一篇文章中,我被告知mysqli已经成功完成了mysql。我已经注意到一些函数中的几乎完全语法,我可以在其中添加一个i到“ MySQL的“;但是,在某些功能中,情况并非如此。但是,如果我要使用相同的mysql函数,将它们更改为mysqli,是否有必要对脚本进行任何重大更改?
谢谢。
答案 0 :(得分:0)
嗨,我认为你需要一个大的重新分解。但为了使其工作,将以下块代码移动到move_uploaded_file
返回true
的位置,并在返回false时返回null
if(file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])){
echo 'File exists! :D';
unlink ($_FILES['upload']['tmp_name']);
}
return $image;
//gives back image where the function was called