Jump to content

help with uploading a picture to MySQL DB


webguync

Recommended Posts

  • Replies 53
  • Created
  • Last Reply

And yet you claim the function is succeeding.  So you're uploading a fresh picture, one you've never used before, and you're seeing it in the directory?  But you don't see an echo or any more errors?

 

That's not really possible.

 

-Dan

you've set error reporting to e_all?

error_reporting(E_ALL);

and try using copy() rather than move_uploaded_file() to see if that works to ensure its not a folder/file permissions problem.

 

and then use file_exists(); to ensure the file exists

i.e.

if

if (file_exists($_FILES['myfile']['tmp_name']) && copy($_FILES['myfile']['tmp_name'], $uploadFile)) {

                $statement = "insert into Profile_Photos (filename) values ('{$uploadFile}')";

                echo "<P>".$statement;

                mysql_query($statement);

                echo "<P>".mysql_error();

[code=php:0]

 

//change copy back to move_uploaded_file(); when you've completed debugging

I think I figured out the problem. Since I took the JS away to get this working, I was still usng the IF statement looking for the JS.  Moved the INSERT up to the 1) JS is turned off and it worked.

 


<?php
ini_set("display_errors","1");
ERROR_REPORTING(E_ALL);

$db_user = "";
$db_pass = "";
$db = "";


$link = mysql_connect('localhost',$db_user,$db_pass);
$db_selected = mysql_select_db($db);
/*debugging*/

if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

$uploadDir = dirname(__FILE__) . '/files/';
$uploadFile = $uploadDir . basename($_FILES['myfile']['name']);
//Print_r ($_FILES);

if ($_POST['submit'] != '') {
    // 1. submitting the html form
    if (!isset($_GET['jqUploader'])) {
        // 1.a javascript off, we need to upload the file
        if (move_uploaded_file ($_FILES['myfile']['tmp_name'], $uploadFile)) {
	 $statement = "insert into Profile_Photos (Path_to_File) values ('{$uploadFile}')";
                echo "<P>".$statement;
                mysql_query($statement);
                echo "<P>".mysql_error();
            // delete the file
            // @unlink ($uploadFile);
            $html_body = '<h1>File successfully uploaded!</h1><pre>';
            $html_body .= print_r($_FILES, true);
            $html_body .= '</pre>';
        } else {
            $html_body = '<h1>File upload error!</h1>';

            switch ($_FILES['myfile']['error']) {
                case 1:
                    $html_body .= 'The file is bigger than this PHP installation allows';
                    break;
                case 2:
                    $html_body .= 'The file is bigger than this form allows';
                    break;
                case 3:
                    $html_body .= 'Only part of the file was uploaded';
                    break;
                case 4:
                    $html_body .= 'No file was uploaded';
                    break;
                default:
                    $html_body .= 'unknown errror';
            }
            $html_body .= 'File data received: <pre>';
            $html_body .= print_r($_FILES, true);
            $html_body .= '</pre>';
        }
        $html_body = '<h1>Results</h1><pre>';
        $html_body .= print_r($_POST, true);
        $html_body .= '</pre>';
    } else {
        // 1.b javascript on, so the file has been uploaded and its filename is in the POST array
        $html_body = '<h1>Form posted!</h1><p>Error:<pre>';
        $html_body .= print_r($_POST, false);
        $html_body .= '</pre>';
    }
    myHtml($html_body);
} else {
    if ($_GET['jqUploader'] == 1) {
        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // 2. performing jqUploader flash upload

        if ($_FILES['myfile']['name']) {
          if (move_uploaded_file ($_FILES['myfile']['tmp_name'], $uploadFile)) {
               // $statement = "insert into Profile_Photos (filename) values ('{$uploadFile}')";
               // echo "<P>".$statement;
               // mysql_query($statement);
               // echo "<P>".mysql_error();
                 //delete the file
                  //@unlink ($uploadFile);
                return $uploadFile;
            }
        } else {
            if ($_FILES['myfile']['error']) {
                return $_FILES['myfile']['error'];
            }
        }
    }
}
// /////////////////// HELPER FUNCTIONS
function myHtml($bodyHtml)
{

    ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>File Upload</title>
<link rel="stylesheet" type="text/css" media="screen" href="style.css"/>
</head>
<body>
<?php echo $bodyHtml;

    ?>
</body>
</html>
<?php
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.