首页 文章

如何将图像上传到服务器并保存其路径

提问于
浏览
-3

我希望能够将图像上传到服务器,但不是将图像保存为数据库中的字节,而是希望保存路径,以便在视图页面中我通过它的路径调用图像 . 还有上传时的图像应该如果找不到文件夹,则上传到某个文件夹中创建one.all这应该是使用MVC5任何帮助真的很感激

1 回答

  • 1

    不确定您想要使用哪种语言,但这是将照片保存到数据库后所需的实现方法 . (基本上你的意思)

    • 照片已压缩

    • 照片被发送到服务器

    • 服务器的api处理文件到文件夹的存储

    • 将folderpath 'filename'保存到数据库中

    这是一个PHP方法upload(),它是处理步骤3和4的服务器API的一部分

    发现于Ray Wenderlich

    //upload API
    function upload($id, $photoData, $title) {
    
        // index.php passes as first parameter to this function $_SESSION['IdUser']
        // $_SESSION['IdUser'] should contain the user id, if the user has already been authorized
        // remember? you store the user id there in the login function
        if (!$id) errorJson('Authorization required');
    
        // check if there was no error during the file upload
        if ($photoData['error']==0) {
    
            // insert the details about the photo to the "photos" table
            $result = query("INSERT INTO photos(IdUser,title) VALUES('%d','%s')", $id, $title);
            if (!$result['error']) {
    
                // fetch the active connection to the database (it's initialized automatically in lib.php)
                global $link;
    
                // get the last automatically generated ID in the photos table
                $IdPhoto = mysqli_insert_id($link);
    
                // move the temporarily stored file to a convenient location
                // your photo is automatically saved by PHP in a temp folder
                // you need to move it over yourself to your own "upload" folder
                if (move_uploaded_file($photoData['tmp_name'], "upload/".$IdPhoto.".jpg")) {
    
                    // file moved, all good, generate thumbnail
                    thumb("upload/".$IdPhoto.".jpg", 180);
    
                    //just print out confirmation to the iPhone app
                    print json_encode(array('successful'=>1));
                } else {
                    //print out an error message to the iPhone app
                    errorJson('Upload on server problem');
                };
    
            } else {
                errorJson('Upload database problem.'.$result['error']);
            }
        } else {
            errorJson('Upload malfunction');
        }
    }
    

相关问题