0

The image file must not be uploading. I'm learning how to build a blog so I am starting simple to get the basic mechanics. There's a form to create an article on one page, a page for processing the form and reporting any errors, and another page for viewing short descriptions of all articles. There's also a file for viewing the uploaded image and another --coming-- to view an individual article in its entirety but they shouldn't be needed here. All the article data goes to a table in a MySQL database.

Whenever I upload the article, I get this message on the processing page: "Image not found in uploaded data" I can remove the ! from this: if (!array_key_exists('image', $_FILES)) { throw new Exception('Image not found in uploaded data'); } ...in the processing file and get "File is not an uploaded file" ...sounds pretty similar. Connecting's not an issue. I'll paste in the relevant files and MySQL table.

This is the form file:

<?php
session_start(); #redirect
if (isset($_SESSION['usr_id']) && !empty($_SESSION['usr_id']) ) {
} else {
    header('Location: notloggedin.php'); #redirect URL
}
?>
<?php include('header.php'); ?>
<div class="container blog-container"><!-- Begin Blog Container-->
    <div class="row"><!-- Begin Blog Row -->
        <div class="col-lg-9 col-sm-12 blog-main-content"><!-- Begin Blog Content Div (Left Column) -->
            <div class="blog-header"><!-- Begin Blog Content Header -->
                <p><g16 class="blog-header-text">THE DATABASE DRIVEN BLOG BY ROBERT</g16></p>
            </div><!-- End Blog Content Header -->
            <div class="blog-content"><!-- Begin - Main blog content in this div -->
                <br><br>
                <p align="center">
        <form metdod="post" action="article_process.php" enctype="multipart/form-data" name="form1">
            <table widtd="100%" border="0" ><!-- TABLE TD'S AND TEXT REFUSE TO OBEY CSS!!! -->
                <tr>
                    <td align="right" valign="top" width="30%" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Title:</g16></td>
                        <td><g14 style="font-size: 12pt;"><input name="title" id="title" type="text" size="60" maxlengtd="75" /></g14></td>
                </tr>
                <tr>
                    <td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Date:</g16></td>
                        <td style=" padding-top: 5px;"><g14 style="font-size: 12pt;"><input type="text" disabled="disabled" value=<?php echo date("Y-m-d"); ?> name="cdate" id="cdate"  /></g14></td>
                </tr>
                <tr>
                    <td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Author:</g16></td>
                        <td style=" padding-top: 5px;"><g14 style="font-size: 12pt;"><input type="text" disabled="disabled" value=<?php echo $_SESSION['usr_id']; ?> name="author" id="author"  /></g14></td>
                </tr>
                <tr>
                    <td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Categories:</g16></td>
                        <td style=" padding-top: 5px;"><g14 style="font-size: 12pt;"><label for="category"></label>
                            <select name="category" id="category">
                                <option value="Corruption"><g14>Corruption</g14></option>
                                <option value="Capitalism"><g14>Capitalism</g14></option>
                                <option value="Fascism"><g14>Fascism</g14></option>
                            </select></g14></td>
                </tr>
                <tr>
                    <td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Short Description:</g16></td>
                        <td style=" padding-top: 5px;"><g12 style="font-size: 12pt;"><input name="desc" type="text" id="desc" size="60" maxlengtd="200" /></g12></td>
                </tr>
                <tr>
                    <td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Article:</g16></td>
                        <td style=" padding-top: 5px;"><g12 style="font-size: 12pt;"><textarea name="article" cols="59" rows="20"></textarea></g12></td>
                </tr>
                <tr>
                    <td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"><g16 style="text-align: right; line-height: 80%; font-size: 16pt; color: black;">Upload File:</g16></td>
                        <td style=" padding-top: 5px;"><g14 style="font-size: 12pt;"><input type="file" name="image" /></g14></td>
                </tr>
                <tr>
                    <td align="right" valign="top" style="padding-left: 15px; padding-right: 10px; padding-top: 10px;"></td>
                        <td style=" padding-top: 5px;"><g14 style="font-size: 12pt;"><input name="submit" type="submit" value="Upload the Article" /><g14><input name="reset" type="reset" value="Reset" /></g14></td>
                </tr>
            </table>
        </form>
    </p>
                <br><br>
            </div><!-- End - Main blog content in this div -->
            <div class="blog-header" style="vertical-align: middle; padding-bottom: 1px;"><!-- Begin Blog Content Footer -->
                <p><g16 class="blog-header-text"><hr></g16></p>
            </div><!-- End Blog Content Footer -->
        </div><!-- End Blog Content Div (Left Column) -->

<!-- End of Body -->
<?php include('side-comments.php'); ?>
<?php include('footer.php'); ?>

This is the file to process the form, article-process.php:

<?php
session_start();
?>

<?php include('header.php'); ?>

<?php
require_once('dbconnect.php');

function assertValidUpload($code)
{
    if ($code == UPLOAD_ERR_OK) {
        return;
    }

    switch ($code) {
        case UPLOAD_ERR_INI_SIZE:
        case UPLOAD_ERR_FORM_SIZE:
            $msg = 'Image is too large';
            break;

        case UPLOAD_ERR_PARTIAL:
            $msg = 'Image was only partially uploaded';
            break;

        case UPLOAD_ERR_NO_FILE:
            $msg = 'No image was uploaded';
            break;

        case UPLOAD_ERR_NO_TMP_DIR:
            $msg = 'Upload folder not found';
            break;

        case UPLOAD_ERR_CANT_WRITE:
            $msg = 'Unable to write uploaded file';
            break;

        case UPLOAD_ERR_EXTENSION:
            $msg = 'Upload failed due to extension';
            break;

        default:
            $msg = 'Unknown error';
    }

    throw new Exception($msg);
}

$errors = array();

try {
    if (!array_key_exists('image', $_FILES)) {
        throw new Exception('Image not found in uploaded data');
    }

    $image = $_FILES['image'];

    // ensure the file was successfully uploaded
    assertValidUpload($image['error']);

    if (!is_uploaded_file($image['tmp_name'])) {
        throw new Exception('File is not an uploaded file');
    }

    $info = getImageSize($image['tmp_name']);

    if (!$info) {
        throw new Exception('File is not an image');
    }
}
catch (Exception $ex) {
    $errors[] = $ex->getMessage();
}

if (count($errors) == 0) {
    // no errors, so insert the image
    $today = date("Y-m-d");
    $story = mysql_real_escape_string($_POST['article']);
    $query = sprintf(
        "insert into article (filename, mime_type, file_size, file_data, title, curr_date, author, description, category, story)
                values ('%s', '%s', %d, '%s', '$_POST[title]','$today', '$_POST[author]', '$_POST[desc]', '$_POST[category]', '$story')",
        mysql_real_escape_string($image['name']),
        mysql_real_escape_string($info['mime']),
        $image['size'],
        mysql_real_escape_string(
            file_get_contents($image['tmp_name'])));

    mysql_query($query, $conn);

    $id = (int) mysql_insert_id($conn);

    // finally, redirect the user to view the new image
    header('Location: thanks-article.php');
    exit;
}
?>
<div class="container blog-container"><!-- Begin Blog Container-->
    <div class="row"><!-- Begin Blog Row -->
        <div class="col-lg-9 col-sm-12 blog-main-content"><!-- Begin Blog Content Div (Left Column) -->
            <div class="blog-header"><!-- Begin Blog Content Header -->
                <p><g16 class="blog-header-text">THE DATABASE DRIVEN BLOG BY ROBERT</g16></p>
            </div><!-- End Blog Content Header -->
            <div class="blog-content blog-content-message"><!-- Begin - Main blog content in this div -->
                <div style="width: 60%; margin-left: auto; margin-right: auto; padding: 15px;"><!-- Begin - Contains Error Messages -->
                    <p><g16 style="font-size: 18pt;">
                        The following errors occurred:
                    </g16></p>
                    <g14 style="font-size: 14pt; color: #cc0000;"><?php foreach ($errors as $error) { ?>
                    <ul>
                            <li>
                                <?php echo htmlSpecialChars($error) ?>
                            </li>
                        <?php } ?>
                    </ul></g14>

                    <p><g14 style="font-size: 14pt;">
                        <a href="index-blog.php">Try again</a>
                    </g14></p>
                </div><!-- End - Contains Error Messages -->
                <div style="width: 40%; margin-left: auto; margin-right: auto; padding: 15px;">
                <table>
                    <tr>
                        <td width="10" align="center" valign="middle">
                        <a href="index-blog.php"><img src='img/c-02.png' onmouseover="this.src='img/c-02-invert.png';" onmouseout="this.src='img/c-02.png';" /></a>
                        </td>
                    </tr>
                </table>
                </div>
            </div><!-- End - Main blog content in this div -->
            <div class="blog-header" style="vertical-align: middle; padding-bottom: 1px;"><!-- Begin Blog Content Footer -->
                <p><g16 class="blog-header-text"><hr></g16></p>
            </div><!-- End Blog Content Footer -->
        </div><!-- End Blog Content Div (Left Column) -->
        <!-- End of Body -->
<?php include('side-comments.php'); ?>
<?php include('footer.php'); ?>

This is the page you are taken to if the article is submitted successfully, thanks-article.php:

<?php
session_start();
?>
<?php include('header.php'); ?>
<div class="container blog-container"><!-- Begin Blog Container-->
    <div class="row"><!-- Begin Blog Row -->
        <div class="col-lg-9 col-sm-12 blog-main-content"><!-- Begin Blog Content Div (Left Column) -->
                        <div class="blog-header"><!-- Begin Blog Content Header -->
                            <p><g16 class="blog-header-text">THE DATABASE DRIVEN BLOG BY ROBERT</g16></p>
                        </div><!-- End Blog Content Header -->
                <div class="blog-content blog-content-message"><!-- Begin - Main blog content in this div -->
                          <p><br></p>
                          <p style="text-align: center;"><g18 style="font-size: 32pt; color: #004d00; text-shadow: 0 0 8px #ffff80, 0 0 16px #b3ff66;">YOU CREATED AN ARTICLE!</g18><br></p>
                    <p style="text-align: center;">Click <a href="view.php">here</a> to view the image you uploaded.</p>
                    <p style="text-align: center;">Click <a href="article.php">here</a> to view your article.</p>
                    <p style="text-align: center;">Click <a href="index-blog-view.php">here</a> to view the article list.</p>
                    <div style="width: 40%; margin-left: auto; margin-right: auto; padding: 15px;">
                        <table>
                            <tr>
                                <td width="auto" align="center" valign="middle">
                                    <img src='img/c-08.png' />
                                </td>
                            </tr>
                        </table>
                    </div>
                </div><!-- End - Main blog content in this div -->
                <div class="blog-header" style="vertical-align: middle; padding-bottom: 1px;"><!-- Begin Blog Content Footer -->
                    <p><g16 class="blog-header-text"><hr></g16></p>
                </div><!-- End Blog Content Footer -->
        </div><!-- End Blog Content Div (Left Column) -->
                    <!-- End of Body -->
<?php include('side-comments.php'); ?>
<?php include('footer.php'); ?>

This is the table from the database:

CREATE TABLE `article` (
  `article_id` bigint(20) NOT NULL,
  `filename` varchar(255) NOT NULL,
  `mime_type` varchar(255) NOT NULL,
  `file_size` int(11) NOT NULL,
  `file_data` longblob NOT NULL,
  `title` varchar(75) NOT NULL,
  `curr_date` varchar(20) NOT NULL,
  `author` varchar(50) NOT NULL,
  `description` varchar(200) NOT NULL,
  `category` varchar(25) NOT NULL,
  `story` longblob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I can add that when I put it on the remote server I get "404 - File or directory not found." with all the same files and table... different connect script. Thank You for Your Help!

Rob Heston
  • 69
  • 1
  • 9
  • I can also add that my CSS would not work when styling everything inside the form. It worked for other things on that page though... – Rob Heston Nov 23 '16 at 02:44
  • Don't save files in a database. http://stackoverflow.com/a/38829952/267540 – e4c5 Nov 23 '16 at 03:13
  • Thanks. I had run across code for doing it that way. That is the way I would prefer to do it! But, this is a class and the instructor insists on doing everything exactly as described. The unfortunate part is that I cannot get any help out of him regarding errors with the code. So, I really need to discover the problem with this. – Rob Heston Nov 23 '16 at 04:21
  • It was a school project. I had built a registration and login system with a lot of features that insure correct input and error message for everything. I was using mysqli. The blog features, including the article upload with an image from the instructor, used mysql (no i). I had to opt out of my fantastic registration stuff and go to mysql for everything. You can't use both. If I create something like that I'll want to use a file directory instead of inserting all that code into a database, but it's probably good to know because I'll probably run into to the old school stuff out there. – Rob Heston Nov 24 '16 at 18:15

0 Answers0