SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot Tjobbe's Avatar
    Join Date
    Sep 2004
    Location
    UK
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php - if no image is uploaded, dont break script!

    Basically, I have a php script that takes form data and stores information into a mysql database, and uploads an image into a folder.

    Currently, if no image is uploaded, then the script breaks, I'd like it to carry on, and enter a blank image that is already uploaded, into the database.

    Here's the php;

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Patrick Lennon Associates - Marketing and public relations consultants</title>
    <link rel="stylesheet" type="text/css" href="/style/style.css" />
    </head>

    <body>
    <div id="all">
        <div id="box" class="clearfix">
            <div id="header">
                <ul>
                    <li><a id="homeLink" href="/cms/">Admin Home</a></li>
                </ul>
            </div>
            <div id="content">
                <?php 
                
                
    include "dbconnect.php"$uploaddir '../images/'
                
    $uploadfile $uploaddir basename($_FILES['userfile']['name']); 
                
                if (
    move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
                   echo 
    "<h1>Image and article successfully stored</h1>"
                
                
    $title=$_POST['title']; 
                
    $shortdescr=$_POST['shortdescr']; 
                
    $source=$_POST['source']; 
                
    $content=$_POST['content'];
                
    $date=$_POST['date']; 
                
    $category=$_POST['category']; 
                
    $imagebasename($_FILES['userfile']['name']); 
                
                
    $sqlquery "INSERT INTO $category VALUES (id,'$title','$shortdescr','$source','$content','$date','$image')"
                
                
    $results mysql_query($sqlquery) or die(mysql_error()); 
                
                } else { 
                   echo 
    "Possible file upload attack!\n"
                } 
                echo 
    "<br /><img style=\"float:right; padding: 0 10px 0 10px;\" src=\"/cms/images/$uploadfile\" />"
                echo 
    "<p><strong>$title</strong></p>"
                echo 
    nl2br("$content"); 
                
    ?>
                <p><a href="/cms/"><img src="/cms/images/backbutton.jpg" alt="Back to the admin home page" width="194" height="101" border="0" /></a></p>
            </div>
            <div id="footer">
                <?php include '/includes/footer.php'?>
            </div>
        </div>
    </div>
    </body>
    </html>
    What do I need to do to tell the script to use the blank image already stored on the folder on the server, to use thta if no image has been uploaded?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Do something akin to this:

    PHP Code:
                if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
                   echo 
    "<h1>Image and article successfully stored</h1>"
                   
    $imagebasename($_FILES['userfile']['name']); 
    }else{

                   echo 
    "<h1>Default blank image and article successfully stored</h1>"
                   
    $image"/pathto/blank.jpg" 
    }

    // then go on and store 

  3. #3
    SitePoint Zealot Tjobbe's Avatar
    Join Date
    Sep 2004
    Location
    UK
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the really quick reply!fixed and done, many thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •