Adding new code to existing PHP to upload pic to directory and save path to db

I have a separate code to upload a picture, give it a unique md5 name, save it to a directory and save the file path in a database. On it’s own, it works perfectly but since I have been trying to work it into the code I already have to allow registering users to add a picture or already registered users to update a picture - however, no matter where I put the code, it will always upload the unique path to the user’s picture field in the database but absolutely will not save the picture in the directory leaving the database linking to a picture that was never actually there.

Basically, I already have all the code I need, I just need help placing it. For the users, I am using a script that is freely available for anyone to download and customize to their needs so it is still available for anyone to download and view to understand how the code works and how it’s laid out. My issue really is that there are so many separate files that are working together, I’m not sure where to place the “if(move_uploaded_file” portion of the script to allow the picture to actually upload and save to the directory. Theres; process.php, session.php, database.php, etc. I figured “database” would be my best bet but can’t get it to work there or anywhere.

My basic picture upload script is as follows:

[php]<?php
//This is the directory where images will be saved
$new_image_name = md5( rand (0, 1000) . rand (0, 1000) . rand (0, 1000) . rand (0, 1000) ) .’.jpg’;
$path = ‘avatars/’ . $new_image_name;

// Connects to your Database
mysql_connect("", "", “*”) or die(mysql_error()) ;
mysql_select_db(“businessdb1”) or die(mysql_error()) ;

//Writes the photo to the server
if(move_uploaded_file($_FILES[‘photo’][‘tmp_name’], $path))
{
//Writes the information to the database
mysql_query(“INSERT INTO employees VALUES (’$path’)”) ;

//Tells you if its all ok
echo “The file has been uploaded, and your information has been added to the directory”;
}
else {

//Gives and error if its not
echo “Sorry, there was a problem uploading your file.”;
}
?>[/php]

In my case, the “$path” & “photo” would probably have to be changed to match the picture field in the mySQL database to be “$bpic” & “bpic”.

Also, since my user script references the database in the file; constants.php, I understand that I can remove the above database connection portion and I also remove the portions that tell you if the upload was a success or not since I have a separate error checking in the user script as well. I have only been trying to implement the:
[php]$new_image_name = md5( rand (0, 1000) . rand (0, 1000) . rand (0, 1000) . rand (0, 1000) ) .’.jpg’;
$path = ‘avatars/’ . $new_image_name;[/php]
and
[php]if(move_uploaded_file($_FILES[‘photo’][‘tmp_name’], $path))
{[/php]
portions since I’m convinced that’s all I need of that particular script.

The user script I am trying to work this into is found here: http://blog.geotitles.com/2011/07/php-login-script/

So the user must be registered to upload images? or can anyone upload them?

Is each employee having their own folder where the image is being saved? Does that folder exist or is it being created if it doesnt? If it is being created insure the CHMOD is allowing the folder to be created.

I edited your post to remove your connection information

does any error log or does the code just fail to upload the image?
is the image size limit in the db preventing the upload?

Sponsor our Newsletter | Privacy Policy | Terms of Service