Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 目录中记录的文件名与数据库中记录的文件名不同_Php_Mysql_File_Upload - Fatal编程技术网

Php 目录中记录的文件名与数据库中记录的文件名不同

Php 目录中记录的文件名与数据库中记录的文件名不同,php,mysql,file,upload,Php,Mysql,File,Upload,我有以下脚本为我的文件名生成一个随机时间戳 $newfilename = round(microtime(true)) . ".jpg"; 但是,上载到mySQL数据库的文件名与我的目录中记录的文件名不同,这导致图像无法显示 例如,有一次,数据库中的图像记录为1441743610.jpg,而目录中的图像记录为1441743609.jpg 这是我的代码: session_start(); $username = $_SESSION['username']; $file_dir = "use

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

 $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
上传到数据库的代码(以及其他数据)


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

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

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

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


h、 u.

我决定将$newfilename存储为会话变量。然后,在插入数据库时将调用此会话变量,从而避免两次调用$newfilename(并再次生成microtime,正如您所解释的),您认为这样可以解决此问题吗
else{move_uploaded_file($_FILES[“hello”][“tmp_name”],“user_pictures/$username/”$newfilename);$_SESSION['filename']=$newfilename;}
Yes这将解决问题。但我不认为将其存储在会话中是一个好主意,会话不是为此目的而创建的,或者可能只是一个变量?所以就像
$thisfilename=$newfilename
但我想知道,如果我再次调用$thisfilename,它会再次计算$newfilename的值,然后它将等于一个新的切片机,还是只引用创建时$newfilename的值?你明白我说的吗?是的,我明白。它不会再计算了。它只会复制那个值,而不会再次计算时间。它再次发生。即使使用此新方法,文件名也会有所不同。奇怪的是,每次发生这种情况时,文件名的差异正好为1。将保存在我的目录中的文件名与保存在数据库中的文件名进行比较:
1441915093.jpg
1441915094.jpg
1441912835.jpg
1441912836.jpg
1441912198.jpg
1441912199.jpg
您认为还会出现什么问题?
$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);