如何在文件前面获取 ID 以进行上传


How to get an id in front of file for upload

首先,我对PHP很陌生,对Jquery更好一些。我设法构建了一个上传iFrame,将图像上传到网上商店的保管箱帐户。

所以有人把一件T恤放在购物车里,然后需要上传一些艺术品。客户点击"上传"并被发送到具有保管箱上传脚本的 iFrame。iFrame 的网址是这样的 -> http://webshop.com/dropbox/index.html?id=10102013-88981

目前为止,一切都好。然而,问题是,当两个人上传具有相同名称的文件时,第一个文件正在更新。所以我需要在文件前面有一个唯一的 ID。该唯一 id 是 url 末尾的参数。

所以问题是如何在 url 的末尾获取 id 以及如何将其放置在上传的图像前面?

理想的是文件名的前缀或将所有内容存储在自己的文件夹中。我尝试了几件事,但我的知识有限,所以任何帮助都非常感谢。

上传脚本:

//Start the upload code.
........ 
......
    if(sizeof($_FILES)===0){
       echo "<li>No files were uploaded.</li>";
       return;
    }
    foreach ($_FILES['ufiles']['name'] as $key => $value) {
            if ($_FILES['ufiles']['error'][$key] !== UPLOAD_ERR_OK) {
                echo $_FILES['ufiles']['name'][$key].' DID NOT upload.';
                return;
            }
            $tmpDir = uniqid('/tmp/DropboxUploader-');
            if (!mkdir($tmpDir)) {
                echo 'Cannot create temporary directory!';
                return;
            }
            $tmpFile = $tmpDir.'/'.str_replace("/'0", '_', $_FILES['ufiles']['name'][$key]);
            if (!move_uploaded_file($_FILES['ufiles']['tmp_name'][$key], $tmpFile)) {
                echo $_FILES['ufiles']['name'][$key].' - Cannot rename uploaded file!';
                return;
            }
    try {
            $uploader = new DropboxUploader($drop_account, $drop_pwd );
            $uploader->upload($tmpFile, $drop_dir);
            echo "<li>".$_FILES['ufiles']['name'][$key]."</li>" ;
           // Clean up
           if (isset($tmpFile) && file_exists($tmpFile))
               unlink($tmpFile);
           if (isset($tmpDir) && file_exists($tmpDir))
               rmdir($tmpDir);
        } catch(Exception $e) {
            $error_msg = htmlspecialchars($e->getMessage());
            if($error_msg === 'Login unsuccessful.' ) {
             echo '<li style="font-weight:bold;color:#ff0000;">Unable to log into Dropbox</li>';
             return;
            }
             if($error_msg === 'DropboxUploader requires the cURL extension.' ) {
             echo '<li style="font-weight:bold;color:#ff0000;">Application error - contact admin.</li>';
             return;
            }
            echo '<li>'.htmlspecialchars($e->getMessage()).'</li>';
        }
    }

根据要求更新

形式:

    <form class="formclass" id="ufileform" method="post" enctype="multipart/form-data">
         <fieldset>
       <div><span class="fileinput"><input type="file" name="ufiles" id="ufiles" size="32" multiple /></span>
</div>
        </fieldset>             
       <button type="button" id="ubutton">Upload</button>
       <button type="button" id="clear5" onclick="ClearUpload();">Delete</button>
        <input type="hidden" name="id" id="prefix" value="" />
            </form>

上传.js(文件可在互联网上作为免费脚本下载(:

(function () {
   if (window.FormData) {
      var thefiles = document.getElementById('ufiles'), upload = document.getElementById('ubutton');//, password = document.getElementById('pbutton');
      formdata = new FormData();
      thefiles.addEventListener("change", function (evt) {
         var files = evt.target.files; //  FileList object
         var i = 0, len = this.files.length, file;
         for ( ; i < len; i++ ) {
            file = this.files[i];
            if (isValidExt(file.name)) {  //if the extension is NOT on the NOT Allowed list, add it and show it.
               formdata.append('ufiles[]', file);
               output.push('<li>', file.name, ' <span class="exsmall">',
                  bytesToSize(file.size, 2),
                  '</span></li>');
               document.getElementById('listfiles').innerHTML = '<ul>' + output.join('') + '</ul>';
            }
         }
         document.getElementById('filemsg').innerHTML = '';
         document.getElementById('filemsgwrap').style.display = 'none';         
         document.getElementById('ubutton').style.display = 'inline-block';
         document.getElementById('clear5').style.display = 'inline-block';
      }, false);
      upload.addEventListener('click', function (evt) {                 //monitors the "upload" button and posts the files when it is clicked
         document.getElementById('progress').style.display = 'block';   //shows progress bar
         document.getElementById('ufileform').style.display = 'none';   //hide file input form
         document.getElementById('filemsg').innerHTML = '';             //resets the file message area
         $.ajax({
            url: 'upload.php',
            type: 'POST',
            data: formdata,
            processData: false,
            contentType: false,
            success: function (results) {
              document.getElementById('ufileform').style.display = 'block';
              document.getElementById('progress').style.display = 'none';
              document.getElementById('filemsgwrap').style.display = 'block';
              document.getElementById('filemsg').innerHTML = '<ul>' + results + '</ul>';
              document.getElementById('listfiles').innerHTML = '<ul><li>Select Files for Upload</ul>';
              document.getElementById('ufiles').value = '';
              document.getElementById('ubutton').style.display = 'none';
              document.getElementById('clear5').style.display = 'none';
              formdata = new FormData();
              output.length = 0;
            }
         });
      }, false);
   } else {
     // document.getElementById('passarea').style.display = 'none';
      document.getElementById('NoMultiUploads').style.display = 'block';
      document.getElementById('NoMultiUploads').innerHTML = '<div>Your browser does not support this application. Try the lastest version of one of these fine browsers</div><ul><li><a href="http://www.mozilla.org" title="Mozilla Firefox"><img src="images/firefox-logo.png" alt="Mozilla Firefox" /></a></li><li><a href="http://www.google.com/chrome" title="Google Chrome"><img src="images/google-chrome-logo.png" alt="Google Chrome Firefox" /></a></li><li><a href="http://www.apple.com/safari/download/" title="Apple Safari"><img src="images/apple-safari-logo.png" alt="Apple Safari" /></a></li><li><a href="http://www.maxthon.com/" title="Maxthon"><img src="images/maxthon-logo.png" alt="Maxthon" /></a></li></ul>';
   }
 document.getElementById('multiload').style.display = 'block';
 document.getElementById('ufileform').style.display = 'block';
}());
function ClearUpload() {  //clears the list of files in the 'Files to Upload' area  and resets everything to be ready for new upload
     formdata = new FormData();
     output.length = 0;
     document.getElementById('ufiles').value = '';
     document.getElementById('listfiles').innerHTML = 'Select Files for Upload';
     document.getElementById('ubutton').style.display = 'none';
     document.getElementById('clear5').style.display = 'none';
     document.getElementById('filemsgwrap').style.display = 'none';
}
function getExtension(filename) {  //Gets the extension of a file name.
     var parts = filename.split('.');
     return parts[parts.length - 1];
}
function isValidExt(filename) {  //Compare the extension to the list of extensions that are NOT allowed.
     var ext = getExtension(filename);
     for(var i=0; i<the_ext.length; i++) {
          if(the_ext[i] == ext) {
               return false;
               break;
          }
     }
return true;
}

更改此行

$tmpFile = $tmpDir.'/'. $_POST['id'] . '-' . str_replace("/'0", '_', $_FILES['ufiles']['name'][$key]);

请注意添加的$_POST['id']

编辑:更改为$ _POST

同样在您发布的表单中添加

<input type="hidden" name="id" value="<?=$_GET['id']; ?>" />

您可以简单地time()文件名。

http://php.net/manual/de/function.time.php

$tmpDir. '/' . time() . str_replace("/'0", '_', $_FILES['ufiles']['name'][$key]);