doudengjin8251 2016-03-03 13:50
浏览 26
已采纳

如何在个人资料图片上传中实现cookie?

I have a problem figuring this out. How to implement cookie in processing uploaded picture name ?

I have 2 files :

index.php (where you can set your profile picture).

 <form action="setPicture.php" method="post" enctype="multipart/form-data" >
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">

setPicture.php

 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }

If a user upload's a profile picture it will be saved in data/img/admin , i must use that source to save it inside the cookie and then get redirected to the main page index.php with the profile picture is set .

Can someone helps me to understand the implementation ?

Here is the complete code of my work. https://jsfiddle.net/u5c4sz6u/1/

  • 写回答

1条回答 默认 最新

  • dongzhuo2371 2016-03-03 18:02
    关注

    Cookie is usually used to record mini temporary data that you want to access later when some interactions occur (ex: pages interaction). In your case, you want to save the cookie's value as the path of the successfully uploaded image.

    In simple case you could just :

    • If the image has been uploaded succesfully, take the path of the file and then save it to a cookie :

       if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
          echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
      
         $full_path =$target_dir.( $_FILES["fileToUpload"]["name"]);
        // set the cookie
       setcookie("mypathvalueissaved",$full_path, time()+3600);  /* expire in 1 hour */ 
       }
      

    now a new cookie created with a name of "mypathvalueissaved" and its value is $full_path which is the the value of the target path + file's name (contains extension).

    • Later on when you want to access the cookie just refer to the cookie's name (even in the different pages, it will be recognized since $_COOKIE is a superglobal array with assumption that you don't set the cookie for a specific domain ) with $_COOKIE["mypathvalueissaved"]. Like when the uploading is successfully, set the cookie then do redirecting and finally set the value of the cookie as the value of a <img src= >, like <img src="<?php echo $_COOKIE['mypathvalueissaved'];?>">.

       // if the cookie with a "mypathvalueissaved" name was successfully created before
       if (isset($_COOKIE["mypathvalueissaved"])){ 
      
         }
      

    This is just a brief explanation, you can improve & expand its usage by yourself.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊