SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File upload not working

    Hoping someone can point out where I have an error in this code. Neither of my 2 files are uploading to the server. The second code is for editing an existing file and that does work properly. As far as I can tell I have similar code, but something isn't working. I have some little snippets here and there as I was testing to see where the code was failing, but it seems it never proceeds past the test for either $_FILES attribute is set (though they should be).

    Thanks in advance, I know it is some stupid oversight on my part but staring at it for the last few hours hasn't fixed it! And feel free to critique any other sections of code, I'll only learn if shown where I error!

    Greg

    <?php
    session_start();
    require("../dbconnect.inc");
    require("inc_variables.inc");
    $PageTitle = "Add New Product";

    if($_SESSION['Active'] != TRUE)
    {
    header ( 'Location:index.php');
    }

    // function to extract the file extension from uploaded files
    function getexts($getfileext) {
    $getfileext = strtolower($getfileext);
    $exts = split("[/\\.]", $getfileext);
    $n = count($exts)-1;
    $exts = $exts[$n];
    return $exts;
    }

    // Test if the form is being reloaded by checking if PartNum is set
    if(isset($_POST['PartNum'])){
    // Start with the image file and move it to proper directory
    if (isset($_FILES['AddPartImage'])) {
    $_SESSION['ImageSuccess'] = "Image 1";
    // Change it to an easier to use variable
    $PartImage = $_FILES['AddPartImage']['name'];
    // the image file needs to be updated
    // first verify it is of file type image
    // Define an array of file types to check against
    $ImageFileTypes = array("gif", "jpg", "jpeg", "png");
    // extract the file extension so we can rename the file
    $ImgExt = getexts($PartImage);
    if (in_array($ImgExt, ($ImageFileTypes))) {
    $ImageToUpload = $imglocation . $_POST['PartNum'] . "." . $ImgExt;
    move_uploaded_file($_FILES['AddPartImage']['tmp_name'], $ImageTUpload) or die("Could not copy image file to server");
    $_SESSION['ImageSuccess'] = " **Image Uploaded** ";
    }
    }
    if (isset($_FILES['AddPDFFile'])) {
    $_SESSION['PDFSuccess'] = "PDF 1";
    // Change it to an easier to use variable
    $PDFFile = $_FILES['AddPDFFile']['name'];
    // the PDF file needs to be updated
    // first verify it is of file type pdf
    $PDFFileTypes = array("pdf");
    $PDFExt = getexts($PDFFile);
    if (in_array($PDFExt, ($PDFFileTypes))) {
    $PDFToUpload = $pdflocation . $_POST['PartNum'] . "." . $PDFExt;
    move_uploaded_file($_FILES['AddPDFFile']['tmp_name'], $PDFTUpload) or die("Could not copy PDF file to server");
    $_SESSION['PDFSuccess'] = " **PDF Uploaded** ";
    }
    }
    // Set Active value to be either 0 or 1 based on checkbox
    if ($_POST['chkActive'] == "Yes") {
    $UpdateActive = 1;
    } else {
    $UpdateActive = 0;
    }

    // Parse the ProdDesc to store in notes field with <br /> values in place of /r/n
    $ChangeThis = array("\r\n");
    $ToThis = "<br />";
    $UpdateNotes = str_replace($ChangeThis, $ToThis, $_POST['ProdDetails']);
    $UpdateDetails = str_replace($ChangeThis, $ToThis, $_POST['ProdDesc']);

    // Input the updated part info into the database and provide feedback to user
    $InsertPartSQL = "INSERT INTO tbl_products ";
    $InsertPartSQL .= "(part_number, cat_id, notes, description, active)";
    $InsertPartSQL .= "VALUES";
    $InsertPartSQL .= "('" . $_POST['PartNum'];
    $InsertPartSQL .= "', " . $_POST['MainCat'];
    $InsertPartSQL .= ", '" . $UpdateNotes;
    $InsertPartSQL .= "', '" . $UpdateDetails;
    $InsertPartSQL .= "', '" . $UpdateActive;
    $InsertPartSQL .= "');";
    mysql_query($InsertPartSQL) or die ($UpdatePartSQL . 'Error updating part in DB&nbsp;' . mysql_error());
    $_SESSION['UserFeedback'] = "Part # '" . $_POST['PartNum'] . "' was added to the database.";
    // Call the process to update the Product List for showing in dropdown boxes
    include ('inc_writeprodlist.inc');
    header ( 'Location:index.php');
    }
    ?>

    <html>
    <head>
    <link rel="stylesheet" href="birk_partadmin.css" type="text/css" />
    <title>Birk Part Admin - <?php echo $PageTitle; ?></title>
    </head>
    <body>
    <!-- Display the Product Admin form -->
    <!-- First the Category Selector -->
    <div id="sitewrapper">
    <img src="../images/birk-logo.gif" width="482" height="234" alt="Birk Aerosystems Logo" />
    <h1><?php echo $PageTitle; ?></h1>
    <?php
    include ("inc_navigation.inc");
    ?>
    <div id="frmWrapper">
    <form name="AddPart" method="post">
    <p id="MainCat_container">
    <label for="MainCatList" id="MainCatList">Select Category:&nbsp;</label>
    <select name="MainCat">
    <?php include("inc_catlist.inc"); ?>
    </select>
    </p>
    <p id="PartNum_container">
    <label for="PartNum" id="PartNumLbl">Enter Part #</label>
    <input type="text" name="PartNum" />
    </p>
    <p id="chkActive_container">
    <label for="chkActive" id="lblchkActive">Active:&nbsp;</label>
    <input type="checkbox" name="chkActive" id="chkActive" checked="checked" />
    </p>
    <p id="ProdDesc_container">
    <label for="ProdDesc" id="ProdDesc">Product Description/label>
    <textarea name="ProdDesc"></textarea>
    </p>
    <p id="ProdDetails_container">
    <label for="ProdDetails" id="ProdDetails">Product Details</label>
    <textarea name="ProdDetails"></textarea>
    </p>
    <p id="ProdImage_container">
    <label for="PartImage" id="PartImage">Part Image/label>
    <!--<img src="emptypart.jpg" width="320" height="240" alt="Product Image" />-->
    <br /><br />
    <input type="file" name="AddPartImage" />
    </p>
    <p id="PDFFile_container">
    <label for="PDFFile" id="PDFFile">PDF File:&nbsp;</label>
    <br /><br />
    <input type="file" name="AddPDFFile" />
    </p>
    <p id="Submit_button">
    <button type="submit">Submit New Part</button>
    <button type="reset" onclick="javascript:history.back();return false;">Cancel Add</button>
    </p>
    </form>
    </div>
    </div>
    </body>
    </html>


    This is the code for the Part Edit, and it does upload the files:
    <?php
    session_start();
    require("../dbconnect.inc");
    require("inc_variables.inc");
    $PageTitle = "Edit Existing Product";

    if($_SESSION['Active'] != TRUE)
    {
    header ( 'Location:index.php');
    }

    if ($_POST['ExistProd'] == "NULL")
    {
    $_SESSION['NavError'] = "Please choose which product to edit";
    header ( 'Location:index.php');
    }

    // define the full path and filename for product image and pdf file
    $prodimage = $imglocation . $_POST['ExistProd'] . ".jpg";
    $prodpdf = $pdflocation . $_POST['ExistProd'] . ".pdf";

    // function to extract the file extension from uploaded files
    function getexts($getfileext) {
    $getfileext = strtolower($getfileext);
    $exts = split("[/\\.]", $getfileext);
    $n = count($exts)-1;
    $exts = $exts[$n];
    return $exts;
    }
    ?>
    <html>
    <head>
    <link rel="stylesheet" href="birk_partadmin.css" type="text/css" />
    <title>Birk Part Admin - <?php echo $PageTitle; ?></title>
    </head>
    <body>
    <?php
    // Test if the form is being reloaded by checking if PartNum is set
    if(isset($_POST['PartNum'])){
    // Start with the image file - is it new?
    if (isset($_FILES['NewPartImage'])) {
    // Change it to an easier to use variable
    $NewPartImage = $_FILES['NewPartImage']['name'];
    // the image file needs to be updated
    // first verify it is of file type image
    // Define an array of file types to check against
    $ImageFileTypes = array("gif", "jpg", "jpeg", "png");
    // extract the file extension so we can rename the file
    $ImgExt = getexts($NewPartImage);
    if (in_array($ImgExt, ($ImageFileTypes))) {
    $ImageToUpload = $imglocation . $_POST['PartNum'] . "." . $ImgExt;
    move_uploaded_file($_FILES['NewPartImage']['tmp_name'], $ImageToUpload) or die("Could not copy new image file to server");
    }
    }
    if (isset($_FILES['NewPDFFile'])) {
    // Change it to an easier to use variable
    $NewPDFFile = $_FILES['NewPDFFile']['name'];
    // the PDF file needs to be updated
    // first verify it is of file type pdf
    $PDFFileTypes = array("pdf");
    $PDFExt = getexts($NewPDFFile);
    if (in_array($PDFExt, ($PDFFileTypes))) {
    $PDFToUpload = $pdflocation . $_POST['PartNum'] . "." . $PDFExt;
    move_uploaded_file($_FILES['NewPDFFile']['tmp_name'], $PDFToUpload) or die("Could not copy new PDF file to server");
    }
    }
    // Set Active value to be either 0 or 1 based on checkbox
    if ($_POST['chkActive'] == "Yes") {
    $UpdateActive = 1;
    } else {
    $UpdateActive = 0;
    }

    // Parse the ProdDesc to store in notes field with <br /> values in place of /r/n
    $ChangeThis = array("\r\n");
    $ToThis = "<br />";
    $UpdateNotes = str_replace($ChangeThis, $ToThis, $_POST['ProdDetails']);
    $UpdateDetails = str_replace($ChangeThis, $ToThis, $_POST['ProdDesc']);

    // Input the updated part info into the database and provide feedback to user
    $UpdatePartSQL = "UPDATE tbl_products ";
    $UpdatePartSQL .= "SET notes = '" . $UpdateNotes;
    $UpdatePartSQL .= "', description = '" . $UpdateDetails;
    $UpdatePartSQL .= "', active = '" . $UpdateActive;
    $UpdatePartSQL .= "' WHERE part_number = '" . $_POST['PartNum'] . "';";
    mysql_query($UpdatePartSQL) or die ($UpdatePartSQL . 'Error updating part in DB&nbsp;' . mysql_error());
    $_SESSION['UserFeedback'] = "Part # '" . $_POST['PartNum'] . "' was updated in the database.";
    include ('inc_writeprodlist.inc');
    header ( 'Location:index.php');
    }

    // Retrieve selected part for editing from db
    $editpartsql = 'SELECT cat_id, notes, description, active FROM tbl_products WHERE part_number = \'' . $_POST['ExistProd'] . '\'';
    $getpartinfo = mysql_query($editpartsql);
    if (!$getpartinfo) die ("Access to database failed for part retrieval: " . mysql_error());
    $partrow = mysql_fetch_array($getpartinfo);
    $partcat = $partrow['cat_id'];
    if ($partrow['active'] == 0){
    $active = '';
    } else {
    $active = ' checked="yes"';
    }
    $partnum = $_POST['ExistProd'];
    $notes = $partrow['notes'];
    $description = $partrow['description'];

    // Retrieve the appropriate category titles

    $getcatsql = 'SELECT root.category_name as root_category_name,
    down1.category_name as down1_category_name,
    down2.category_name as down2_category_name,
    down3.category_name as down3_category_name,
    root.product_id as root_product_id,
    down1.product_id as down1_product_id,
    down2.product_id as down2_product_id,
    down3.product_id as down3_product_id
    FROM tbl_category as root
    LEFT OUTER JOIN tbl_category as down1
    on down1.product_id = root.parent_id
    LEFT OUTER JOIN tbl_category as down2
    on down2.product_id = down1.parent_id
    LEFT OUTER JOIN tbl_category as down3
    on down3.product_id = down2.parent_id
    WHERE root.product_id = \'' . $partcat . '\'';
    $getcatname1 = mysql_query($getcatsql);
    if (!$getcatname1) die ("Access to database failed for category part level:&nbsp;" . mysql_error());
    $catrow = mysql_fetch_array($getcatname1);
    $catname1 = $catrow['root_category_name'];
    $catid1 = $catrow['root_product_id'];
    $catname2 = $catrow['down1_category_name'];
    $catid2 = $catrow['down1_product_id'];
    $catname3 = $catrow['down2_category_name'];
    $catid3 = $catrow['down2_product_id'];
    $catname4 = $catrow['down3_category_name'];
    $catid4 = $catrow['down3_product_id'];
    $catid = $partcat;

    $partcat = "";

    if ($catname4 != NULL) {
    $partcat .= $catname4 . "&nbsp;-&nbsp;";
    }
    if ($catname3 != NULL) {
    $partcat .= $catname3 . "&nbsp;-&nbsp;";
    }
    if ($catname2 != NULL) {
    $partcat .= $catname2 . "&nbsp;-&nbsp;";
    }
    if ($catname1 != NULL) {
    $partcat .= $catname1;
    }
    $lookfor = array("<br />", "<br>");
    $replace = "\r\n";
    $shownotes = str_replace($lookfor, $replace, $notes);
    $showdescription = str_replace($lookfor, $replace, $description);
    ?>

    <!-- Display the Product Admin form -->
    <!-- First the Category Selector -->
    <div id="sitewrapper">
    <img src="../images/birk-logo.gif" width="482" height="234" alt="Birk Aerosystems Logo" />
    <h1><?php echo $PageTitle; ?></h1>
    <?php
    include ("inc_navigation.inc");
    ?>

    <div id="frmWrapper">
    <form name="EditPart" method="post" enctype="multipart/form-data">
    <p id="MainCat_container">
    <label for="MainCatList" id="MainCatList">Category:&nbsp;</label>
    <?php echo $partcat; ?>
    </p>
    <p id="PartNum_container">
    <label for="PartNum" id="PartNumLbl">Part #</label>
    <input type="hidden" name="PartNum" value="<?php echo $partnum; ?>">
    <?php echo $partnum; ?>
    </p>
    <p id="chkActive_container">
    <label for="chkActive" id="lblchkActive">Active:&nbsp;</label>
    <input type="checkbox" name="chkActive" id="chkActive" value="Yes" <?php echo $active; ?> />
    </p>
    <p id="ProdDesc_container">
    <label for="ProdDesc" id="ProdDesc">Product Description/label>
    <textarea name="ProdDesc"><?php echo $showdescription; ?></textarea>
    </p>
    <p id="ProdDetails_container">
    <label for="ProdDetails" id="ProdDetails">Product Details</label>
    <textarea name="ProdDetails"><?php echo $shownotes; ?></textarea>
    </p>
    <p id="ProdImage_container">
    <label for="CurrentPartImg" id="CurrentPartImg">Current Part Image/label>
    <img src="<?php echo $prodimage; ?>" width="320" height="240" alt="<?php echo $prodimage; ?>&nbsp;Product Image" />
    <br /><br />
    <label for="NewPartImg" id="NewPartImage">Change Picture/label>
    <input type="file" name="NewPartImage" />
    </p>
    <p id="PDFFile_container">
    <label for="CurrentPDFFile" id="CurrentPDFFile">
    Current PDF:&nbsp;
    </label>
    <a href="<?php echo $prodpdf; ?>" target="_blank">
    <?php echo $prodpdf; ?>
    </a>
    <br /><br />
    <input type="file" name="NewPDFFile" />
    <label for="NewPDFFile" id="NewPDFFile">
    Change PDF:&nbsp;
    </label>
    </p>
    <p id="Submit_button">
    <button type="submit">Edit Existing Part</button>
    <button type="reset" onclick="javascript:history.back();return false;">Cancel Edit</button>
    </p>
    </form>
    </div>
    </div>
    </form>
    </body>
    </html>

  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)
    Neither of my 2 files are uploading to the server.
    Tackle that first.

    get your form to post to simple testuploadpage.php


    <form name="EditPart" method="post" enctype="multipart/form-data" action = "testuploadpage.php">

    that test form handler should just contain:

    PHP Code:
    <?php
    var_dump
    ($_POST);
    var_dump($_FILES);
    Now inspect the output carefully, especially the value of "error" which should be 0, if it is not, get back the manual and look up the error number.

  3. #3
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for that. I am still teaching myself PHP and forgot all about the var_dump testing method. However, I think in your reply you may have hit on it. I realized that in my Add Part form (that isn't uploading the files) I don't have the enctype="multipart/form-data" declaration but on the Edit Part form I do. That is probably what the problem is and the one section of the code I hadn't pored over time and time again. But that var_dump method will be helpful (if I can manage to remember) for the future!

    As always, the Sitepoint forums are the ONE AND ONLY place I even ask for answers anymore.

    Greg

  4. #4
    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)
    Well I'm glad you got to the bottom of it.

    var_dump()ing your variables onto the page as you work will help you understand what PHP is being passed in the case of POST vars etc, and also what variables actually contain vs what you think they should contain.


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
  •