Jump to content

Recommended Posts

Hello! I'm trying to create a page for users to upload text and images to a MYSQL database. However, I  I'd like to store only the image information in the database, but move the image itself in a folder in an image directory on the website. I hope that makes sense so far.

 

The code I have so far is intended to store the text information in the database and upload the image directly to a folder on the website called /images. I'm having trouble getting it to work. First, here is the HTML form for the user to input the data:

 

<html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>post</title>
    </head>
    <body>
        POST<br/><br/>
        

        <form action="posted.php" method="post" enctype="multipart/form-data">
                title:<br />  <input type="text" name="title" size="93"/> (optional)<br />
                date: <br/> <input type="text" name="date" /><br/>
                post:<br />   <textarea rows="100" cols="80" name="post"></textarea><br />
                pictures: <br/> <input type="file" name="picture" accept="image/jpeg"/>
		<input type="submit" name="Submit" value="do it"/><br />

        </form>

        
    </body>
</html>

 

and here is the PHP file (posted.php) :

 

<?php

$target = "/images";
$target = $target . basename ($_FILES['picture']);

$title = $_POST['title'];
$post = $_POST['post'];
$date = $_POST['date'];
$picture = $_FILES['picture'];


mysql_connect("--", "--", "--") or die(mysql_error()) ;
mysql_select_db("dbname") or die(mysql_error()) ;

mysql_query("INSERT INTO Posts (Number,Title,Content,Date,Picture)
VALUES (null,'$title', '$post', '$date', '$picture')") ;

if(move_uploaded_file $_FILES['picture'], $target)
{

echo "The file ". basename $_FILES['picture']. " has been uploaded, and your information has been added to the directory";
}
else {

echo "Sorry, there was a problem uploading your file.";
}
?>

 

The result is the message "I'm sorry, there was a problem uploading your file" I'm fairly sure the problem is with the $target or $_FILES variables. I know the connection to MYSQL works because I've gotten a different form to work that uploads only text to the database, but the images are posing a problem. I'm not sure I understand the basic concept for moving an image file to a remote directory. Could someone help me with the syntax?

 

Thanks so much!

 

Link to comment
https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/
Share on other sites

Try this if your permissions are fine:

 

if(move_uploaded_file($_FILES['picture'], $target))
{

echo "The file ". basename($_FILES['picture']). " has been uploaded, and your information has been added to the directory";
}
else {

echo "Sorry, there was a problem uploading your file.";

 

That would explain why it would be failing the first check and jumping to the else.

Depends on your FTP program.  If you use say...Firefox and FireFTP...you just simply right click on the images folder and set permissions.  Most cases in FTP programs...you would just right click on the file or folder and choose to set the permissions.  You could get a way with 755..but if not you would need to set the permissions to 777.

angelcool - elvia is talking about the form information into the database...not the image itself.  There is a form with information about the image that is being submitted to the database.  The image is being stored in the images directory.

thanks for the suggestions! i made sure that the images folder and subfolders were readable/writable/executable by anyone, and tried adding the missing sets of parentheses to the code as bubbasheeko suggested. BUT unfortunately still no luck...  could it be a problem with the way i have my target folder formatted?

 

here's the code with the new parentheses:

<?php

$target = "/images/posts";
$target = $target . basename ($_FILES['picture']);

$title = $_POST['title'];
$post = $_POST['post'];
$date = $_POST['date'];
$picture = $_FILES['picture'];


mysql_connect("--", "--", "--", "--") or die(mysql_error()) ;
mysql_select_db("dbname") or die(mysql_error()) ;

mysql_query("INSERT INTO Posts (Number,Title,Content,Date,Picture)
VALUES (null,'$title', '$post', '$date', '$picture')") ;

if(move_uploaded_file ($_FILES['picture'], $target))
{

echo "The file ". basename ($_FILES['picture']). " has been uploaded, and your information has been added to the directory";
}
else {

echo "Sorry, there was a problem uploading your file.";
}
?>

I think there might also be a conflict with the $target variable in the move_uploaded_file() function,

 

try:

 

$target='/images/posts/'.$_FILES['userfile']['name'];
move_uploaded_file ($_FILES['picture']['tmp_name'],$target)

 

Also check you /tmp directory in your box, all uploaded files get temporarly store here; So after submitting a file (with your HTML form), monitor your /tmp directory and see if a file is created (with a weird name).

 

:-)

I'll quote myselft,

 

Also check you /tmp directory in your box, all uploaded files get temporarly store here; So after submitting a file (with your HTML form), monitor your /tmp directory and see if a file is created (with a weird name).

 

This behavior occurs where PHP sessions are stored (session.save_path in php.ini); this does not seem the same with upload_tmp_dir, as I ran an upload script, file successfully uploaded but did not see the same behavior, perhaps someone can confirm this.

 

I guess I got this two parameters mixed up when first posted this.

 

Angel

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.