php 下载图像并将其放在几个不同的文件夹中

好吧,我的要求是每个选中的复选框都会发送所选的图像,但我不知道除了“move_uploaded_file”之外还有什么其他方式。也许只是需要我的代码,或者只是我的请求无法工作。我想到了一个选项,可以复制图像并将其发送到请求的文件夹。感谢您阅读我的问题。


PHP


   // Create database connection

    $db = mysqli_connect("localhost", "root", "", "photos");

    // Initialize message variable

    $msg = "";


    // If upload button is clicked ...

    if (isset($_POST['upload'])) {

        // Get image name

        $image = $_FILES['image']['name'];

        //GET image nom

        $image_nom = mysqli_real_escape_string($db, $_POST['image_nom']);

        // Get text

        $image_text = mysqli_real_escape_string($db, $_POST['image_text']);

        // image file directory

        if (isset($_POST['combat'])) {

            $target = "combat/images/".basename($image);

        

            if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {

                    $msg = "Image uploaded successfully";

            }else{

                    $msg = "Failed to upload image";

            }

            $sql = "INSERT INTO images (image, image_nom, image_text) VALUES ('$image', '$image_nom', '$image_text')";

        // execute query

        mysqli_query($db, $sql);

        }

        

        if (isset($_POST['personnage'])) {

            $targetp = "search/personnages/".basename($image);

        

            if (move_uploaded_file($_FILES['image']['tmp_name'], $targetp)) {

                    $msg = "Image uploaded successfully";

            }else{

                    $msg = "Failed to upload image";

            }

            $sql = "INSERT INTO personnage (image_personnage, image_nom_personnage, image_text_personnage) VALUES ('$image', '$image_nom', '$image_text')";

        // execute query

        mysqli_query($db, $sql);

        }

和 HTML


<form method='post' action='' enctype='multipart/form-data'>

  <input type="checkbox" id="combat" name="combat">

  <label for="combat">Combat</label></br>


  <input type="checkbox" id="personnage" name="personnage">

  <label for="personnage">Personnage</label></form>


米脂
浏览 87回答 1
1回答

守着星空守着你

move_uploaded_file() 只是移动上传的文件,你可以使用它一次,但你可以使用 copy() 函数复制文件多次,就像这个例子一样move_uploaded_file($uploaded, $destination);copy($destination, $destination2);但是您的代码存在重大安全问题,因为您允许上传每种类型的文件,这可能使任何人都可以破解您的网站。我建议你:添加accept='image/*'到input type='file'您的 html 中。通过以下代码检查文件扩展名和 MIME 类型:$ext=explode(".",$_FILES['image']['name'][$i]);$ext=strtolower($ext[1]);if ((($_FILES['image']['type'][$i] == "image/gif")|| ($_FILES['image']['type'][$i] == "image/jpeg")|| ($_FILES['image']['type'][$i] == "image/jpg")|| ($_FILES['image']['type'][$i] == "image/pjpeg")|| ($_FILES['image']['type'][$i] == "image/x-png")|| ($_FILES['image']['type'][$i] == "image/png"))&& ($_FILES['image']['size'][$i] < 5000000)&& in_array($ext,array("gif","jpeg","jpg","png"))){}然后从临时文件创建图像:switch ($ext){ case 'jpg':$image=imagecreatefromjpeg($_FILES['image']['tmp_name'][$i]);break; case 'jpeg':$image=imagecreatefromjpeg($_FILES['image']['tmp_name'][$i]);break; case 'png':$image=imagecreatefrompng($_FILES['image']['tmp_name'][$i]);break; case 'gif':$image=imagecreatefromgif($_FILES['image']['tmp_name'][$i]);break; }使用imagejpeg($image,$path,75)或imagepng($image,$path,8)创建图像并将其保存在路径中,您可以随意使用这两个功能,并且imagedestroy($image)完成后不要忘记使用。如果您还需要什么,请告诉我。编辑:添加新代码if($_FILES['image']['name']!=''){$ext=explode(".",$_FILES['image']['name']);$ext=strtolower($ext[1]);if ((($_FILES['image']['type'] == "image/gif")|| ($_FILES['image']['type'] == "image/jpeg")|| ($_FILES['image']['type'] == "image/jpg")|| ($_FILES['image']['type'] == "image/pjpeg")|| ($_FILES['image']['type'] == "image/x-png")|| ($_FILES['image']['type'] == "image/png"))&& ($_FILES['image']['size'] < 12000000)&& in_array($ext,array("gif","jpeg","jpg","png"))){$is_jpg=true;$ex='jpg';switch ($ext){case 'jpg':$image=imagecreatefromjpeg($_FILES['image']['tmp_name']);break;case 'jpeg':$image=imagecreatefromjpeg($_FILES['image']['tmp_name']);break;case 'png':$image=imagecreatefrompng($_FILES['image']['tmp_name'][$i]);$ex='png';$is_jpg=false;break;case 'gif':$image=imagecreatefromgif($_FILES['image']['tmp_name']);$ex='gif';$is_jpg=false;break;default:$image=imagecreatefromjpeg($_FILES['image']['tmp_name']);break;}//set random name for img to avoid duplicate name$img_name='upload_'.mb_substr(md5($_FILES['image']['name'].rand(0,50)),0,rand(5,10)).'.'.$ex;$width=imagesx($image);$height=imagesy($image);//if image jpgif($is_jpg){//change to your upload pathisset($_POST['combat']){imagejpeg($image,'combat/images/'.$img_name,80);}isset($_POST['personnage']){imagejpeg($image,'search/personnages/'.$img_name,80);}imagedestroy($image);}// if image png or gif to save alpha . if gif will not be animatedelse{$bg = imagecreatetruecolor($width, $height);imagesavealpha($bg, TRUE);imagefill($bg, 0, 0, imagecolorallocatealpha($bg, 0, 0, 0,127));imagecopy($bg, $image, 0, 0, 0, 0,$width, $height);imagedestroy($image);isset($_POST['combat']){imagepng($bg,'combat/images/'.$img_name,8);}isset($_POST['personnage']){imagepng($bg,'search/personnages/'.$img_name,8);}imagedestroy($bg);}}}
打开App,查看更多内容
随时随地看视频慕课网APP