Free Web Hosting Forum
Go Back   Free Web Hosting Forum > Website Building > Web Programming
Reload this Page Stuck trying to learn php and mysql (image uploading issue)
Reply
 
Thread Tools Display Modes
(#1 (permalink))
Old
Junior Member
ug02fast is on a distinguished road
 
Posts: 2
Join Date: Apr 2012
Default Stuck trying to learn php and mysql (image uploading issue) - 04-25-2012, 10:16 PM

Hi everyone, i've recently been trying to learn some php and mysql. I have actually been working from a book called PHP & MySQL the missing manual. Well i got far and was doing good until my images would not display after uploading.

Could anyone take a look at my code and point out what is wrong? I'm really noob at this whole thing, I got the image to upload to my sql table but the image doesn't display after uploading.

PHP Code:
<?php

require_once 'app_config.php';
require_once 
'database_connection.php';

$upload_dir SITE_ROOT "uploads/profile_pics/";
$image_fieldname "user_pic";

// Potential PHP upload errors
$php_errors = array(=> 'Maximum file size in php.ini exceeded',
                    
=> 'Maximum file size in HTML form exceeded',
                    
=> 'Only part of the file was uploaded',
                    
=> 'No file was selected to upload.');

$first_name trim($_REQUEST['first_name']);
$last_name trim($_REQUEST['last_name']);
$email trim($_REQUEST['email']);
$bio trim($_REQUEST['bio']);
$facebook_url str_replace("facebook.org""facebook.com"trim($_REQUEST['facebook_url']));
$position strpos($facebook_url"facebook.com");
if (
$position === false) {
  
$facebook_url "http://www.facebook.com/" $facebook_url;
}

$twitter_handle trim($_REQUEST['twitter_handle']);
$twitter_url "http://www.twitter.com/";
$position strpos($twitter_handle"@");
if (
$position === false) {
  
$twitter_url $twitter_url $twitter_handle;
} else {
  
$twitter_url $twitter_url substr($twitter_handle$position 1);
}

// Make sure we didn't have an error uploading the image
($_FILES[$image_fieldname]['error'] == 0)
  or 
handle_error("the server couldn't upload the image you selected.",
                  
$php_errors[$_FILES[$image_fieldname]['error']]);

// Is this file the result of a valid upload?
@is_uploaded_file($_FILES[$image_fieldname]['tmp_name'])
  or 
handle_error("you were trying to do something naughty. Shame on you!",
                  
"Uploaded request: file named '{$_FILES[$image_fieldname]['tmp_name']}'");

// Is this actually an image?
@getimagesize($_FILES[$image_fieldname]['tmp_name'])
  or 
handle_error("you selected a file for your picture that isn't an image.",
                  
"{$_FILES[$image_fieldname]['tmp_name']} isn't a valid image file.");

// Name the file uniquely
$now time();
while (
file_exists($upload_filename $upload_dir $now .
                                     
'-' .
                                     
$_FILES[$image_fieldname]['name'])) {
    
$now++;
}

echo 
$upload_filename;
echo 
"<br />";
echo 
$_FILES[$image_fieldname]['tmp_name'];

// Finally, move the file to its permanent location
@move_uploaded_file($_FILES[$image_fieldname]['tmp_name'], $upload_filename)
  or 
handle_error("we had a problem saving your image to its permanent location.",
                  
"permissions or related error moving file to {$upload_filename}");

$insert_sql "INSERT INTO users (first_name, last_name, email, bio, facebook_url, twitter_handle, user_pic_path) " .
              
"VALUES ('{$first_name}', '{$last_name}', '{$email}', '{$bio}', " .
                      
"'{$facebook_url}', '{$twitter_handle}', '{$upload_filename}');";

// Insert the user into the database
mysql_query($insert_sql);

// Redirect the user to the page that displays user information
header("Location: show_user.php?user_id=" mysql_insert_id());
?>
When I upload the image it shows up as a broken image and does not display the image.

I personally think it has something to do with
$upload_dir = SITE_ROOT . "uploads/profile_pics/";

but I am just not sure.
The site is http://splash.comuf.com/create_user.html if you could look at that aswell.

I really really appreciate if anyone could help me out.

Last edited by ug02fast; 04-25-2012 at 10:18 PM.
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 732
Join Date: Dec 2010
Default 04-26-2012, 02:46 AM

Quote:
I personally think it has something to do with
$upload_dir = SITE_ROOT . "uploads/profile_pics/";
Please try after changing as shown below:

$upload_dir = dirname(_FILE_)."/uploads/profile_pics";

Last edited by grace1004; 04-26-2012 at 03:00 AM.
Reply With Quote
(#3 (permalink))
Old
Junior Member
ug02fast is on a distinguished road
 
Posts: 2
Join Date: Apr 2012
Default 04-26-2012, 03:29 AM

Quote:
Originally Posted by grace1004 View Post
Please try after changing as shown below:

$upload_dir = dirname(_FILE_)."/uploads/profile_pics";

I love you.

Works!

Thank you so much!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com