简体   繁体   English

PHP上传pdf文件并用自定义文件名替换文件名

[英]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:我在您的代码中看到了几个问题:

  1. Use __DIR__ to properly define location of uploads directory使用__DIR__正确定义uploads目录的位置
  2. Check that directory exists and is writable检查目录是否存在且可写
  3. Unclear what 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:你可能想看看:

  • A router library to properly handle different HTTP routes.正确处理不同 HTTP 路由的路由器库
  • An incoming HTTP request handling library (or even a simple framework that comes with router, request handling, validation etc.)一个传入的 HTTP 请求处理库(甚至是一个简单的框架,带有路由器、请求处理、验证等)
  • A file system handling library to abstract from using FS directly.一个文件系统处理库,可以从直接使用 FS 中抽象出来。
  • A form validation library to validate user input (since it's UNSAFE and INSECURE to allow users define their own names for uploaded files without validation )用于验证用户输入的表单验证库(因为它是不安全和不安全的,允许用户在没有验证的情况下为上传的文件定义自己的名称)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM