简体   繁体   English

PHP映像上传脚本未上传但未失败

[英]PHP Image Upload Script Not Uploading But Not Failing

I am trying to enter some data into a database and upload an image to a specific directory. 我正在尝试将一些数据输入数据库并将图像上传到特定目录。 I am using the following script which is a modified version of the top voted answer to this question: How to store file name in database, with other info while uploading image to server using PHP? 我正在使用以下脚本,该脚本是此问题的最高投票答案的修改版本: 如何在使用PHP将映像上传到服务器时,如何将文件名与其他信息一起存储在数据库中?

require($_SERVER['DOCUMENT_ROOT']."/settings/functions.php");
// This is the directory where images will be saved
$target = $_SERVER['DOCUMENT_ROOT']."/safetyarticles/images/";
$target = $target . basename( $_FILES['article_img']['name']);
date_default_timezone_set("America/Chicago");

// This gets all the other information from the form
$article_name = $_POST['article_title'];
$article_date = date("m/d/Y");
$article_creator = $_POST['article_creator'];
$article_views = "0";
$article_content = $_POST['article_content'];
$article_content_2 = $_POST['article_content_2'];
$article_img = ($_FILES['article_img']['name']);
$article_credit = $_POST['article_credit'];


// Connect to database
$conn = getConnected("safetyArticles");

// moves the image
if(move_uploaded_file($_FILES['article_img']['tmp_name'], $target))     
{
// if upload is a success query data into db 
mysqli_query($conn, "INSERT INTO currentArticles (article_name, article_date, article_creator, article_content, article_content_2, article_img, article_credit)
VALUES ('$article_name', '$article_date', '$article_creator', '$article_views', '$article_content', '$article_content_2', '$article_img', '$article_credit')") ;

echo "The file ". basename( $_FILES['article_img']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}

I have successfully connected to my database since my getConnected() function contains error handling for if the connection fails. 由于getConnected()函数包含错误处理(如果连接失败getConnected()我已成功连接到数据库。

For some reason I keep getting the Sorry, there was a problem uploading your file. 由于某些原因,我一直Sorry, there was a problem uploading your file. error from the bottom of the script. 脚本底部的错误。

Am I missing something? 我想念什么吗? All I did was change some minor lines here and there such as how the database connects, and the variables. 我所做的就是在这里和那里更改一些次要行,例如数据库的连接方式和变量。 I also moved the query to only happen if the file uploads. 我还移动了查询,使其仅在文件上传时才发生。

I'm not sure what I'm missing. 我不确定我缺少什么。

Is it also possible to modify this current script to rename the image to whatever the value of $article_name is? 是否还可以修改当前脚本以将图像重命名为$article_name的值? For example if the article name is "This Is The First Article" then the image would be this-is-the-first-article.jpg ? 例如,如果文章名称是“ This Is The First Article”,则图像将是this-is-the-first-article.jpg

My HTML form is: 我的HTML表单是:

<form method="post" action="http://example.com/admin/articleCreate.php" enctype='multipart/form-data'>
  <input type="text" name="article_title" placeholder="What Is The Name Of This Article?" id="article_title_input">
  <textarea name="article_content" placeholder="Write The Top Half Of Your Article Here." id="article_content_input"></textarea>
  <input type="file" name="article_img" placeholder="If The Article Has An Image, Upload It Here." id="article_img_input">
  <textarea name="article_content_2" placeholder="Write The Bottom Half Of Your Article Here." id="article_content_2_input"></textarea>
  <input type="text" name="article_creator" placeholder="Who Is Writing This Article?" id="article_creator_input">
  <input type="text" name="article_credit" placeholder="If This Article Is Copied, What Website Was It Taken From?" id="article_credit_input">
  <input type="submit" value="Submit">
 </form>

And I did var_dump(is_uploaded_file($_FILES['article_img']['tmp_name'])); 我做了var_dump(is_uploaded_file($_FILES['article_img']['tmp_name'])); and it's returnign true . 这是true

Sidenote edit: This being before you edited your question with only one of them being renamed. 旁注编辑:这是在您编辑问题之前,只有其中一个被重命名的问题。 https://stackoverflow.com/revisions/36367407/4 https://stackoverflow.com/revisions/36367407/4

  • $_FILES['photo']
  • $_FILES['uploadedfile']

are two different file arrays and you're using name="article_img" as the name attribute. 是两个不同的文件数组,并且您使用name="article_img"作为name属性。

You need to use the same one for all of them. 您需要对所有这些都使用相同的。

Error reporting http://php.net/manual/en/function.error-reporting.php would have told you about it. 错误报告http://php.net/manual/zh-CN/function.error-reporting.php会告诉您。

Add error reporting to the top of your file(s) which will help find errors. 错误报告添加到文件顶部,这将有助于发现错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production. 旁注:显示错误仅应在登台中进行,而不应在生产中进行。


Additional edit: 附加编辑:

$target = $target . basename( $_FILES['photo']['name']);

if that's your real edit, still the wrong array name. 如果这是您真正的编辑,仍然是错误的数组名称。

I think the problem is in this line: 我认为问题出在这一行:

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))

Change it to this: 更改为此:

if(move_uploaded_file($_FILES['article_img']['tmp_name'], $target))    

Your html has: 您的html有:

<input type="file" name="article_img" placeholder="If The Article Has An Image, Upload It Here." id="article_img_input">

And your php is waiting for $_FILES['photo']['tmp_name'] 而且您的php正在等待$ _FILES ['photo'] ['tmp_name']

Change your html file input to: 将您的html文件输入更改为:

<input type="file" name="photo" placeholder="If The Article Has An Image, Upload It Here." id="article_img_input">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM