0

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?

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.

For some reason I keep getting the 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? For example if the article name is "This Is The First Article" then the image would be this-is-the-first-article.jpg?

My HTML form is:

<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'])); and it's returnign true.

Community
  • 1
  • 1
Jesse Elser
  • 974
  • 2
  • 11
  • 39

3 Answers3

0

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))    
hayzem
  • 67
  • 1
  • 8
0

Your html has:

<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']

Change your html file input to:

<input type="file" name="photo" placeholder="If The Article Has An Image, Upload It Here." id="article_img_input">
Seva Kalashnikov
  • 4,262
  • 2
  • 21
  • 36
0

Sidenote edit: This being before you edited your question with only one of them being renamed. 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.

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.

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.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Yep just fixed that one too. I completely spaced that the name in the code was different than mine, but still not working. – Jesse Elser Apr 01 '16 at 23:56
  • @JesseElser then add error reporting as I shown you in my answer. If that doesn't yield anything, I will delete my answer. – Funk Forty Niner Apr 01 '16 at 23:57
  • @JesseElser another thing is that your MySQL might also be failing you. Add `or die(mysqli_error($conn))` to `mysqli_query()`. – Funk Forty Niner Apr 01 '16 at 23:58
  • @JesseElser and your edit still contains `$target = $target . basename( $_FILES['photo']['name']);` something I noted in an additional edit in my answer. – Funk Forty Niner Apr 01 '16 at 23:59
  • I changed it in the wrong part. In my code it's all the same though. – Jesse Elser Apr 02 '16 at 00:01
  • @JesseElser all I can make of this at this point is that your MySQL might be failing because it is inside the same conditional statement as your move_uploaded_file. MySQL might be failing you silently if the columns' lengths aren't long enough to accomodate the ingoing data, or that there are characters being introduced that MySQL may be complaining about, but you're not checking for errors on your query. If you're trying to insert the file as a BLOB into db, then you need to escape it, or check the column's length if it's just to store the filename. – Funk Forty Niner Apr 02 '16 at 00:06
  • @JesseElser Aahhhh I just saw your comment now as I was typing my other comment here. Glad to see you found the error. For which one though? – Funk Forty Niner Apr 02 '16 at 00:06
  • I had actually missed a `'` when changing a line but once I fixed that it works like a charm. Thanks. – Jesse Elser Apr 02 '16 at 00:06