AJAX PHP将文件上传到数据库

时间:2014-06-19 16:34:01

标签: php sybase

我正在编写一个上传文件的脚本,通过ajax调用php脚本,将其上传到服务器上的文件夹。这部分工作正常。接下来的步骤就是把这个文件插入到一个数据库中(我知道它不是最佳实践,但我不得不放弃db / software约束)但我似乎无法让它工作。

我会省略ajax,因为它工作正常,这是PHP:

$upload_dir = "./uploads";

$result["status"]  = "200";
$result["message"] = "ERROR!";

if (isset($_FILES['file']))
{
    echo "Uploading File...<br />";
    if ($_FILES['file']['error'] == UPLOAD_ERR_OK) 
    {
        $filename    = $_FILES['file']['name'];
        $destination = 'uploads/' . $filename;
        move_uploaded_file($_FILES['file']['tmp_name'], $destination);

        //THIS IS THE SECTION THAT DOESN'T WORK CORRECTLY. IT JUST DOES NOTHING AT ALL
        //upload the image as a blob
        $image = file_get_contents ($destination);

        //see if there is anything already stored in blob
        $sqlcheck = "select id from blobstore where id='$custid'";
        $result   = sasql_query($connect, "$sqlcheck");

        if (!isset($row['id']))
        {
            $sql = "update blobstore set class='j', id='$custid', blob='".sasql_real_escape_string($connect,$image)."', createdby='$userid', createdat='". date_format($date, 'Y-m-d H:i')."' where id='$custid' ";
            $insert = sasql_query($connect, "$sql");
        }
        else if (isset($row['id']))
        {
            $sql = "insert into blobstore (class, id, blob, createdby, createdat) VALUES ('j','$custid', '".sasql_real_escape_string($connect,$image)."','$userid', '". date_format($date, 'Y-m-d H:i')."') ";
            $insert = sasql_query($connect, "$sql");
        }

        //WHEN I INCLUDE THE ABOVE SECTION TO UPLOAD THE IMAGE TO THE DB THIS ALSO RETURNS NOTHING, WHEREAS IF I TAKE OUT THAT SECTION IT RETURNS AS EXPECTED
        $result["status"]  = "100";
        $result["message"] = "File was uploaded successfully!";
    }
} 
elseif ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE) 
{
    $result["status"]  = "200";
    $result["message"] = "The file is too big.";    
} 
else 
{
    $result["status"]  = "500";
    $result["message"] = "Unknown error.";
}

当我运行此操作时,我收到错误

  

警告:不能使用标量值

当我删除以下代码时,会出现此错误

  

$ result [“status”] =“100”;               $ result [“message”] =“文件已成功上传!”;

非常感谢任何帮助。 感谢

0 个答案:

没有答案