【发布时间】:2018-05-04 19:44:26
【问题描述】:
我正在尝试将图片添加到我的表单中,但似乎无法正常工作。我浏览了我能找到的所有资源,但我无法解决这个问题。我的文件夹是可写的,存在的。 move_uploaded_file(tmpfileName, fileDestination) 的语法也是正确的。 这是我的代码:
if($_FILES['thread_image']['name']!=""){
$fileExtBreak = explode('.', $_FILES['thread_image']['name']);
$fileExt = strtolower(end($fileExtBreak));
$allowed = array('jpg','jpeg','png','pdf');
if(in_array($fileExt, $allowed)){
$_SESSION['errors'] = $errors;
if($_FILES['thread_image']['error'] > 0){
$errors[] = 'Something went wrong when trying to upload your image.';
$_SESSION['errors'] = $errors;
header("Location:topic.php?id=". $_GET['id'] ."");
}else{
if($_FILES['thread_image']['size']>=10000000){
$errors[] = 'The file you are trying to upload is too big';
$_SESSION['errors'] = $errors;
header("Location:topic.php?id=". $_GET['id'] ."");
}else{
if(!is_dir('images')){
$errors[]='This shit doesn\'t exist';
}else if(!is_writable('images')){
$errors[]='This shit isn\'t writable';
}else{
$errors[]='It exists and should work!!!!';
}
$fileName = $date.'.'.$fileExt;
$filePath = 'images/'.$fileName;
$errors[]=$_FILES['thread_image']['tmp_name'];
$errors[]=$fileName;
$errors[]=$filePath;
if(move_uploaded_file($_FILES['thread_image']['tmp_name'], $filePath)){
$errors[]="success";
}else{
$errors[]="didn't work, sorry";
}
$errors[]=mysqli_error($link);
$_SESSION['errors'] = $errors;
$stmt = $link->prepare("UPDATE threads set thread_has_image='1' WHERE thread_date=?");
$stmt->bind_param("s",$date);
$result=$stmt->execute();
}
}
}else{
$errors[] = "You cannot upload files with that type";
$_SESSION['errors'] = $errors;
header("Location:topic.php?id=". $_GET['id'] ."");
}
}
这是它吐出来的:
1) 检查文件夹是否存在,2) 吐出临时文件名 3) 吐出我创建的文件名,4) 吐出我创建的文件 Destination 5) 检查 move_uploaded_file 是否有效
这是我的表格:
<form class="makerContainer" method="post" enctype="multipart/form-data" action=<?php echo '"threadMaker_handler.php?id=' . $_GET[ 'id'] . '"'; ?>>
<fieldset class="good">
<div class="makerForm">
Thread Title:
<input type="text" class="title" title="ThreadTitle" name="thread_title" <?php echo 'value="' . $name . '"'; ?>>
</div>
<div class="makerForm">
Thread Description:
<textarea class="description" rows="8" cols="100" name="thread"><?php echo '' . $thread . ''; ?></textarea>
<div class="submitPanel">
<input type="file" name="thread_image"/>
<input type="submit" class="button buttonModal buttonColor" value="+ Create Thread">
</div>
</div>
</fieldset>
</form>
【问题讨论】:
-
使用 php 的错误报告并确保您的服务器确实支持您正在使用的命名约定
-
@FunkFortyNiner 你是说mysqli_error($link)吗?因为我正在使用它。它什么也没吐出来。 “您的服务器确实支持您使用的命名约定”是什么意思?
-
@TalhaAhmed 他的意思是stackoverflow.com/questions/845021/…
-
我的意思是文件名的冒号。表格是什么样的?
标签: php database forms image upload