[英]PHP Upload pdf file and replace the file name with a custom one
I have a problem, I can't get php to replace the name of the pdf with "$fiscale".我有一个问题,我无法让 php 用“$fiscale”替换 pdf 的名称。 I am a new php user and I would need a hand.
我是一个新的 php 用户,我需要帮助。 Thanks to everyone for the possible answers: 3 (I am creating a site that allows you to upload pdfs with a name chosen at the beginning.)
感谢大家提供可能的答案:3(我正在创建一个网站,允许您使用开头选择的名称上传 pdf。)
php: php:
<?php
// per prima cosa verifico che il file sia stato effettivamente caricato
if (!isset($_FILES['userfile']) || !is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo 'File non caricato.';
exit;
}
//percorso della cartella dove mettere i file caricati dagli utenti
$uploaddir = "uploads/" . $_POST['fiscale'] . '.pdf';
//recupero il nome originale del file caricato
$userfile_name = $_FILES['userfile']['name'];
$fiscale = $_POST['fiscale'];
//copio il file dalla sua posizione temporanea alla mia cartella upload
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir)) {
//Se l'operazione è andata a buon fine...
echo 'File inviato con successo.';
}else{
//Se l'operazione è fallta...
echo 'Upload NON valido!';
}
?>
html: html:
<form enctype="multipart/form-data" action="upload.php" method="POST">
Invia questo file: <input name="userfile" type="file">
<input name="fiscale" type="text" placeholder="test">
<input type="submit" value="Invia File">
</form>
Try using:尝试使用:
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir)) {
instead of代替
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir)) {
Regarding your question关于你的问题
I see several issues in your code:我在您的代码中看到了几个问题:
__DIR__
to properly define location of uploads
directory__DIR__
正确定义uploads
目录的位置move_uploaded_file
does.move_uploaded_file
作用。 Friendly advice友好的建议
You should definitely start using open source tools for your need.您绝对应该开始使用开源工具来满足您的需要。 You might want to have a look at:
你可能想看看:
Use composer .使用作曲家。 Use contemporary tools.
使用现代工具。
Do everyone that comes after you, including your future self, a favor — use ready-made libraries to handle typical problems, most probably all your problems have been solved already and put into open source solutions.帮助你之后的每一个人,包括你未来的自己——使用现成的库来处理典型的问题,很可能你所有的问题都已经解决并放入开源解决方案中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.