Jump to content

PHP file upload save to directory save path to db help


dspurg7310

Recommended Posts

Trying to find a way to combine two scripts.

1. picture upload script that uploads the picture, gives it a new unique name, saves it to a specified directory and saves the unique name path to a user database (basically a user profile picture upload).

and

2. A larger user login script I already have worked into my website that allows users to register on the site, view and edit their profile, log in, log out, etc.

 

The issue is in part that I don't understand PHP well enough to figure out how to work these in together and the other part is that they are written a bit differently from each other so it's making it hard to combine them.

 

One of the user profile fields I am using with script 2 is valued "bpic" and is where path to the uploaded picture would go. I have been successful in getting the unique path to save in that field in the database but absolutely can NOT seem to get the picture to actually upload and save in the "avatars" directory no matter what I try. I have determined that because my user script already calls the database, has an "INSERT INTO" database command and has error and success messages, I could take out those aspects of the picture uploading code below so I'm just trying to figure out where to place the "$new_image_name =" and "$bpic = " lines and how and where to code in the "move_uploaded_file" portion so the file actually uploads lol.

 

(These are the two portions of the script below that I have decided I need to use and find a place to paste):

$new_image_name = md5( rand (0, 1000) . rand (0, 1000) . rand (0, 1000) . rand (0, 1000) ) .'.jpg';
$bpic = 'avatars/' . $new_image_name;

 if(move_uploaded_file($_FILES['bpic']['tmp_name'], $bpic))
 {
}

Any help and direction is appreciated! I have been at this for weeks with no success. It took me a while to actually get the picture upload script to work on it's own and upload the file, give it a unique name and save that unique name in both the directory and database so it can be found and displayed correctly. But anyway, the upload picture script works perfect now on it's own, but I gotta figure out how to place it in the script I already have.

 

Upload Picture Script: (note: "bpic" is the field name of the file upload on the form and "$bpic" both references the field in the database and the file path to be created for the pic.)

<?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';
$bpic = 'avatars/' . $new_image_name;
 
 // Connects to your Database 
 mysql_connect("businessdb1.db.9878324.hostedresource.com", "businessdb1", "Spidey148!") or die(mysql_error()) ; 
 mysql_select_db("businessdb1") or die(mysql_error()) ; 
 
 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['bpic']['tmp_name'], $bpic))
 {
//Writes the information to the database 
 mysql_query("INSERT INTO `employees` VALUES ('$bpic')") ;
 
 //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."; 
 } 
 ?>

As far as the user upload script goes, I'm not sure which page to work this code into so I don't want to make this post insanely long by posting every potential page (database.php - which I think is it, process.php, session.php, constants.php). The exact same code I am using is downloadable for free here: http://blog.geotitles.com/2011/07/php-login-script/ so you can see exactly how my code is formatted.

 

I understand that's a lot of information and actually quite a bit of work for someone to look at and offer help, but if anyone is up to it, I would really appreciate any advice and help with this as I have been pulling out my hair trying to figure this out and it's going to completing a website that a lot of bands are waiting to sign up for! Thanks so much for your time!

 

-David.

Link to comment
Share on other sites

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.