gpt4 book ai didi

php - 如何避免使用空格和/或特殊字符的文件名

转载 作者:行者123 更新时间:2023-11-28 20:06:24 27 4
gpt4 key购买 nike

我有一个网络表单来上传用户的图片。然后我正在创建一个 iOS 应用程序来显示从用户加载的图片。但如果文件名包含空格或特殊字符(如 á、é、í、ó、ú、ñ 等),应用程序不会加载图片,大多数用户来自西类牙...

这是我使用的代码:

<?php if ((isset($_POST["enviado"])) && ($_POST["enviado"] == "form1")) {
$randomString = substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 1) . substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10);

echo $randomString;
$nombre_archivo = $_FILES['userfile']['name'];
move_uploaded_file($_FILES['userfile']['tmp_name'], "logos/".$randomString.$nombre_archivo);

?>

我正在使用随机函数来避免重复文件名。

我如何更改用户给定的文件名,其中可能包含空格和/或特殊字符,以一种可以完美加载到 iOS 应用程序中的方式?

最佳答案

上传后重命名文件。这是对评论中 OP 问题的回答

// get the uploaded file's temp filename
$tmp_filename = $_FILES['userfile']['tmp_name'];

// get the file's extension
$path = $_FILES['userfile']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);

// rename the uploaded file with a timestamp, insert the destination directory and add the extension
$new_filename = 'logos/'.date('Ymdhms').'.'.$ext;

// put the renamed file in the destination directory
move_uploaded_file($tmp_filename, $new_filename);

编辑:每个 OP 问题的新答案

<?php
if((isset($_POST["enviado"])) && ($_POST["enviado"] == "form1")) {
// get the uploaded file's temp filename
$tmp_filename = $_FILES['userfile']['tmp_name'];

// get the file's extension
$path = $_FILES['userfile']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);

// rename the uploaded with a timestamp file, add the extension and assign the directory
$new_filename = 'logos/'.date('Ymdhms').'.'.$ext;

// put the renamed file in the destination directory
move_uploaded_file($tmp_filename, $new_filename);
}

关于php - 如何避免使用空格和/或特殊字符的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21979399/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com