【问题标题】:File name recorded in directory differs from database目录中记录的文件名与数据库不同
【发布时间】:2015-12-04 17:26:14
【问题描述】:

我有以下脚本可以为我的文件名生成一个随机时间戳。

 $newfilename = round(microtime(true)) . ".jpg";

但是上传到我的mySQL数据库的文件名和我的目录中记录的不同,导致图片无法显示。

例如,有一次,数据库中的图像记录为1441743610.jpg,而目录中的图像记录为1441743609.jpg

这是我的代码:

session_start();

$username = $_SESSION['username'];

$file_dir = "userpictures/$username" ;

mkdir($file_dir, 0777, true);

// $_FILES["file"]["name'] is the file that is uploaded by her

$filePath = $_FILES["file"]["name"];
$image = imagecreatefrompng($filePath);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
$quality = 100; // 0 = worst / smaller file, 100 = better / bigger file 
imagejpeg($bg, $filePath . ".jpg", $quality);
imagedestroy($bg);

// this chunk here converts the file to jpg


$newfilename = round(microtime(true)) . ".jpg";
// round(microtime) generates a randomized number.


// check format of image before uploading
$imageFileType = pathinfo($newfilename,PATHINFO_EXTENSION);

if ($imageFileType != "jpg" && $imageFileType != "png" &&   $imageFileType != "jpeg"
&& $imageFileType != "gif" ) 

{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}

else

{
$uploadOk = 1;
}


// if any errors have occured (determined by $uploadOk), the critical "move_uploaded_files" php function is not activated

if ($uploadOk == 1) 

{
$filename = $newfilename;
move_uploaded_file($_FILES["file"]["tmp_name"], "user_pictures/$username/" . $filename);
}

希望能就发生这种情况的原因提供建议。请注意,这种行为并不一致,它大部分时间都有效。,但这对我来说是不可接受的。

编辑:

这是我将$filename 上传到数据库(以及其他数据)的代码

$con = mysqli_connect("FARM.mysql.anyhost.com", "user", "password");
  if (!$con) {
  die("Something went wrong. Don't worry, just refresh the page"); }

mysqli_select_db($con, "FARM");
  if (mysqli_select_db($con, "FARM") == false) {
  die("Something went wrong. Don't worry, just refresh the page"); }

$username = $_SESSION['username'];
$post = mysqli_real_escape_string($con, $_POST['postdescription']);
$timestamp = date("jS \of F Y h:i:s A P");


$user_uploadposts = "INSERT INTO $username (PostingTime, Image, Description)  VALUES ('$timestamp','$filename', '$post')";


mysqli_query($con, $user_uploadposts);
mysqli_close($con);

【问题讨论】:

    标签: php mysql file upload


    【解决方案1】:

    我不知道在数据库中保存文件路径时您的查询如何,但根据您在此处解释的行为:

    请注意,这种行为并不一致,它在大多数情况下都有效。但我不能接受。

    问题是您在保存到数据库时没有使用 $newfilename,而是使用 $newfilename = round(microtime(true)) . ".jpg"; 再次“重新创建”文件名

    这导致同时微时间在增加并且名称不再相等。它有时起作用的原因是因为脚本有时运行得更快。为避免这种情况,您只需使用 $newfilename 而不是再次创建变量。

    呵呵

    【讨论】:

    • 我决定将 $newfilename 存储为 SESSION 变量。然后在插入数据库时​​调用这个 SESSION 变量,从而避免调用 $newfilename 两次(并再次生成微时间,正如你所解释的)你认为这会解决它吗? else{ move_uploaded_file($_FILES["hello"]["tmp_name"], "user_pictures/$username/" . $newfilename); $_SESSION['filename'] = $newfilename;}
    • 是的,这将解决问题。但我认为将其存储在会话中并不是一个好主意,会话不是为此目的而创建的。
    • 或者只是一个变量?就像$thisfilename = $newfilename 但我想知道,如果我再次调用 $thisfilename,它是否会再次计算 $newfilename 的值,这将等于一个新的切片机,或者它只会引用 $newfilename 的值它创建的时间?你明白我在说什么吗?
    • 是的,我明白了。它不会再次计算它。它只会复制该值而无需再次计算时间。
    • 又发生了。即使使用这种新方法,文件名也不同。奇怪的是,每次发生的文件名都相差 1。比较保存在我的目录中的文件名与保存在数据库中的文件名:1441915093.jpg | 1441915094.jpg 1441912835.jpg | 1441912836.jpg 1441912198.jpg | 1441912199.jpg你认为这又是什么问题?
    猜你喜欢
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多