【问题标题】:saving the file path for multiple image upload in sql在sql中保存多张图片上传的文件路径
【发布时间】:2014-07-21 04:21:39
【问题描述】:

我使用以下代码将多个图像上传到位于服务器中的文件夹每个文件将被重命名为随机方法现在我需要的是我只想存储图像文件路径 SQL 数据库这里是我的代码

<?php
class Upload_Rename{
const ALLOWED_TYPES = "jpg,gif,png";
public static function   generate_new_name($extension,$uppercase=true,$prefix='',$sufix=''){
    $new_name = $prefix.uniqid().'_'.time().$sufix;
    return ($uppercase ? strtoupper($new_name) : $new_name).'.'.$extension;
}
public static function check_and_get_extension($file){
    $file_part      = pathinfo($file);
    $allowed_types  = explode(",",Upload_Rename::ALLOWED_TYPES);
    if(!in_array($file_part['extension'], $allowed_types)){
        throw new Exception('Not ok.. bad bad file type.');
    }
    return $file_part['extension'];
}
public function upload($file,$target_destination){
    if(!isset($file['tmp_name'])){
        throw new Exception('Whaaaat?');
    }
    $_name   = $file['name'];
    $_tmp    = $file['tmp_name'];
    $_type   = $file['type'];
    $_size   = $file['size'];
    $file_extension = '';
    try{
        $file_extension = Upload_Rename::check_and_get_extension($_name);
    }catch(Exception $e){
        throw new Exception('Ops.. file extension? what? '.$e->getMessage());
    }
    $new_name    =   Upload_Rename::generate_new_name($file_extension,true,'whaat_','_okey');
    $destination = $target_destination . DIRECTORY_SEPARATOR . $new_name;
    return move_uploaded_file($_tmp, $destination);
}
public function multiple_files($files,$destination){
    $number_of_files = isset($files['tmp_name']) ? sizeof($files['tmp_name']) : 0;
    $errors = array();
    for($i=0;$i<$number_of_files;$i++){
        if(isset($files['tmp_name'][$i]) && !empty($files['tmp_name'][$i])){
            try{
                $this->upload(array(
                    'name'=>$files['name'][$i],
                    'tmp_name'=>$files['tmp_name'][$i],
                    'size'=>$files['size'][$i],
                    'type'=>$files['type'][$i]
                ),$destination);
            }catch(Exception $e){
                array_push($errors,array('file'=>$files['name'][$i],'error'=>$e->getMessage()));
            }
        }
    }
    print_r($errors);
}
}
if($_FILES){
$upload = new Upload_Rename();
$destination ='upload/';
$upload->multiple_files($_FILES['myfile'],$destination);
}
?>

<form  method="post" enctype="multipart/form-data">
<?php for($i=0;$i<10;$i++): ?>
file: <input type="file" name="myfile[]"><br>
<?php endfor; ?>
<input type="submit">
</form>

我需要在 SQL 中存储图像路径以在另一个 PHP 页面中检索它

【问题讨论】:

    标签: php sql database image forms


    【解决方案1】:

    你应该在你的 mysql 表中创建一个列。 假设您将其命名为 image_path。 我将在这里向您展示的是一个伪代码,因此您必须根据需要对其进行调整。 我可以看到你有方法

    public function multiple_files($files,$destination)
    

    我想如果成功则返回 true,否则将引发异常。 您应该做的是创建一个方法来将路径保存在 DB 中的 image_path 字段中。

    所以,在这部分之后:

    $destination ='upload/';
    $upload->multiple_files($_FILES['myfile'],$destination);    
    

    添加新创建的方法,例如。保存路径($路径)

    $path = $destination . '/' . $file_name;
    $this->savePath($path)
    

    你应该很高兴。 因为我不知道你是如何处理你的数据库连接和记录那部分应该在你身上的。

    最好的问候 弗拉基米尔

    【讨论】:

    • 如何将文件路径保存到sql数据库
    • 您将其保存为任何其他记录。所以,创建一个单独的方法,它将记录数据。如果您不知道如何连接到数据库并插入记录,请通过“如何使用 php 在 sql 中插入记录”进行一点谷歌搜索,这应该会对您有所帮助。
    猜你喜欢
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多