We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,664 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Php upload file

Hi there guys, I seem to be having a silly issue and I cant figure out why, my code is working fine for uploading and storing the image name in the datebase, but my issue come about when the form gets sumitted with no file selected I do want this to happen! but I would like a hidden field to be used as the file name that is update the database with "spacer.png"

if $photoname = empty then use this string "spacer.jpg"

Carry on and insert into datebase

Any help would be appreciated?

thanks in advance

<?php


//This is the directory where images will be saved
$target = "memberFiles/$userid/blessings/";
$target = $target . basename( $_FILES['photo']['name']);
$spacer = "spacer.png";
//This gets all the other information from the form
$photoname=($_FILES['photo']['name']);

//Connect to the database through our include 
include_once "connect_to_mysql.php";



// Place Session variable 'id' into local variable
$id = $_SESSION['id'];
// Process the form if it is submitted
if ($_POST['blessing1']) {


    $blessing1 = mysql_real_escape_string(     $_POST['blessing1']);
    $userid = $_POST['userid'];

    $sql1 = mysql_query("INSERT INTO blessings (userid, blessing1, photo, datepublished) 
        VALUES('$userid','$blessing1','$photoname', now())") or die (mysql_error());



//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
header('Location: view_blessings.php');
}

        header('Location: view_blessings.php');

exit();
} // close if post
?>
2
Contributors
2
Replies
1 Hour
Discussion Span
11 Months Ago
Last Updated
3
Views
richlacey
Newbie Poster
7 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

i didn't use php for a long time but i guess if you put this code in line 20 would work it out if you mean with your question to change the empty name with 'spacer.png'

if ($_FILES['photo']['name']!=""){$photoname=$_FILES['photo']['name'];}
else {$photoname=$spacer;}
memomk
Light Poster
44 posts since Oct 2011
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0

Thank you, that worked perfectly. I really appreciate you help

richlacey
Newbie Poster
7 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0636 seconds using 2.69MB