使用PHP重命名和上载文件时出错-未定义索引

使用PHP重命名和上载文件时出错-未定义索引,php,uploading,renaming,Php,Uploading,Renaming,下面是给定的代码。我想在上传之前重命名文件。提交时显示错误:未定义索引 if($_FILES['FPScreenShot']['name']==true) { $SPPic = ($_FILES['FPScreenShot']['name']); $curTime = time(); $NewPriorPic = "prior"; $NewPriorPic = $New

下面是给定的代码。我想在上传之前重命名文件。提交时显示错误:未定义索引

        if($_FILES['FPScreenShot']['name']==true)
        {
            $SPPic = ($_FILES['FPScreenShot']['name']);
            $curTime = time();
            $NewPriorPic = "prior";
            $NewPriorPic = $NewPriorPic.$SGeiNo;
            $NewPriorPic = $NewPriorPic.$SSurgDt;
            $NewPriorPic = $NewPriorPic.$curTime;

            move_uploaded_file($_FILES['FPScreenShot']['tmp_name'] , "upload_pictures/".$_FILES['$NewPriorPic']['name']);
        }
    else
        {
            $SPPic = "NIL";
        }

我觉得你把这句话搞砸了:

(您忘记了起始报价)

因此,将其更改为:

 move_uploaded_file($_FILES['FPScreenShot']['tmp_name'] , "upload_pictures/" . $_FILES[$NewPriorPic]['name']);

您的代码中存在一些语法错误,您的逻辑似乎有点混淆您试图实现的目标,但您可以尝试以下方法:

 <?php    
  if($_FILES['FPScreenShot']['name']) {
            $SPPic = ($_FILES['FPScreenShot']['name']);
            $curTime = time();
            $NewPriorPic = "prior";
            //Add aditional details to the file name
            $NewPriorPic .= $SGeiNo;
            $NewPriorPic .= $SSurgDt;
            $NewPriorPic .= $curTime;
            //Try to move uploaded file   
            if (move_uploaded_file($_FILES['FPScreenShot']['tmp_name'] , "upload_pictures/".$_FILES[$NewPriorPic]['name'])) {
               echo "File successfully uploaded.";  
            }
            else {
               echo "Error while uploading the file.";
            }
        }
    else {
            $SPPic = "NIL";
        }

已解决。如有需要,可供日后参考

    if($_FILES['FPScreenShot']['name']==true)
        {
            $SPPic = ($_FILES['FPScreenShot']['name']);
            $ext = pathinfo($SPPic, PATHINFO_EXTENSION);
            $curTime = time();
            $NewPriorPic = "prior";
            $NewPriorPic = $NewPriorPic.$SGeiNo;
            $NewPriorPic = $NewPriorPic.$SSurgDt;
            $NewPriorPic = $NewPriorPic.$curTime;
            $NewPriorPic = $NewPriorPic.".".$ext;
            $location = "upload_pictures/";

            move_uploaded_file($_FILES['FPScreenShot']['tmp_name'], $location.$NewPriorPic);
        }

您的代码包含语法错误。可能重复检查您在
move\u uploaded\u文件开头的行中引号的使用情况(
quotes在那里,可能是在复制粘贴过程中我弄糟了。让我编辑它。@Sandhu您准确地得到了哪个错误?在哪一行?@Sandhu更新了我的答案!
    if($_FILES['FPScreenShot']['name']==true)
        {
            $SPPic = ($_FILES['FPScreenShot']['name']);
            $ext = pathinfo($SPPic, PATHINFO_EXTENSION);
            $curTime = time();
            $NewPriorPic = "prior";
            $NewPriorPic = $NewPriorPic.$SGeiNo;
            $NewPriorPic = $NewPriorPic.$SSurgDt;
            $NewPriorPic = $NewPriorPic.$curTime;
            $NewPriorPic = $NewPriorPic.".".$ext;
            $location = "upload_pictures/";

            move_uploaded_file($_FILES['FPScreenShot']['tmp_name'], $location.$NewPriorPic);
        }