SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $nid not passing through script

    I have an upload image script, for news posting. The first step is to choose what news post you want to upload an image with, then it brings you to the upload page with the id of the news post. Now, at the top of the page, I echo the id, and everything appears fine. Then you choose a file to upload, and it also inserts the data into a table. Everything works fine, image name, about, except for nid (which is what I have passed through the URL). It just won't insert the $nid.

    Here is that part:

    PHP Code:
    <?
    include("/webap/xampp/htdocs/v4/includes/db_connect.php");
    if (
    is_uploaded_file($imgfile)) {
    $Query "INSERT INTO pics VALUES('0', '$nid', '$imgfile_name', 'about')";
    if (
    mysql_db_query ($DBName$Query$Link)) {
         print (
    "The query was successfully executed! '$nid'<BR>\n");
    } else {
         print (
    "The query could not be executed!<BR>\n");

    }
    ?>
    Like I said, everything but $nid works fine.

    Here is the whole script:

    PHP Code:
    <?
    $nid 
    $_GET['nid'];
    echo
    '
    '
    .$nid.'
    '
    ;
    ?>
    <html>

    <head>
        <title>web.blazonry : PHP : Upload and Resize an Image</title>

    <meta name="author" content="Marcus Kazmierczak, [email]marcus@mkaz.com[/email]">
    <meta name="copyright" content="(c) 2000 mkaz.com">
    <meta name="license" content="http://blazonry.com/mklicense.php">
    <meta name="origin" content="May 2000">

    <?

    if ($REQUEST_METHOD == "POST")
    {

        
    /* SUBMITTED INFORMATION - use what you need
         * temporary filename (pointer): $imgfile
         * original filename           : $imgfile_name
         * size of uploaded file       : $imgfile_size
         * mime-type of uploaded file  : $imgfile_type
         */

         /*== upload directory where the file will be stored
              relative to where script is run ==*/
        
        
    $uploaddir ".";
        

        
    /*== get file extension (fn at bottom of script) ==*/
        /*== checks to see if image file, if not do not allow upload ==*/
        
    $pext getFileExtension($imgfile_name);
        
    $pext strtolower($pext);
        if ((
    $pext != "jpg")  && ($pext != "jpeg"))
        {
            print 
    "<h1>ERROR</h1>Image Extension Unknown.<br>";
            print 
    "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
            print 
    "The file you uploaded had the following extension: $pext</p>\n";

            
    /*== delete uploaded file ==*/
            
    unlink($imgfile);
            exit();
        }


        
    /*== setup final file location and name ==*/
        /*== change spaces to underscores in filename  ==*/
        
    $final_filename str_replace(" ""_"$imgfile_name);
        
    $newfile $uploaddir "/$final_filename";
        
        
    /*== do extra security check to prevent malicious abuse==*/
        
    if (is_uploaded_file($imgfile))
        {

           
    /*== move file to proper directory ==*/
           
    if (!copy($imgfile,"$newfile"))
           {
              
    /*== if an error occurs the file could not
                   be written, read or possibly does not exist ==*/
              
    print "Error Uploading File.";
              exit();
           }
         }

        
    /*== delete the temporary uploaded file ==*/
        
    unlink($imgfile);

        
        print(
    "<img src=\"$final_filename\">");

        
    /*== DO WHATEVER ELSE YOU WANT
             SUCH AS INSERT DATA INTO A DATABASE  ==*/

    }
    ?>


    </head>
    <body bgcolor="#FFFFFF">
    <?=$imgfile_name?>

        <h2>Upload and Resize an Image</H2>

        <form action="<?=$SCRIPT_NAME?>" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="50000">

        <p>Upload Image: <input type="file" name="imgfile"><br>
        <font size="1">Click browse to upload a local file</font><br>
        <br>
        <input type="submit" value="Upload Image">
        </form>

    </body>
    </html>

    <?
        
    /*== FUNCTIONS ==*/

        
    function getFileExtension($str) {

            
    $i strrpos($str,".");
            if (!
    $i) { return ""; }

            
    $l strlen($str) - $i;
            
    $ext substr($str,$i+1,$l);

            return 
    $ext;

        }
    ?>

    <?
    include("/webap/xampp/htdocs/v4/includes/db_connect.php");
    if (
    is_uploaded_file($imgfile)) {
    $Query "INSERT INTO pics VALUES('0', '$nid', '$imgfile_name', 'about')";
    if (
    mysql_db_query ($DBName$Query$Link)) {
         print (
    "The query was successfully executed! '$nid'<BR>\n");
    } else {
         print (
    "The query could not be executed!<BR>\n");

    }
    ?>

  2. #2
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait a second, I think I got it... the upload form was not passing the nid variable with it.


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
  •