简体   繁体   English

在php中打开动态文件

[英]Opening dynamic file in php

I need to open files uploaded by users in blender (for data retrieval and processing). 我需要在Blender中打开用户上传的文件(用于数据检索和处理)。

So far creating $cmd and running it with exec(); 到目前为止,创建$ cmd并使用exec()运行它; has worked for testing but is useless to open files uploaded by users since the file being run is always predefined, (C:\\xampp\\htdocs\\test.new\\test.stl). 已用于测试,但由于打开的文件始终是预定义的(C:\\ xampp \\ htdocs \\ test.new \\ test.stl),因此无法打开用户上传的文件。

$cmd = '"C:\Users\DJ LX\Documents\blender.old\blender.exe" C:\xampp\htdocs\test.new\test.stl"';

if ((($_FILES["file"]["type"] == "application/netfabb")
|| ($_FILES["file"]["type"] == "application/sla")
|| ($_FILES["file"]["type"] == "fileapplication/sla")
|| ($_FILES["file"]["type"] == "fileapplication/vnd.ms-pki.stl")))
  {
  if ($_FILES["file"]["error"] > 0)
   {
   echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
   }
  else
  {
  echo "Upload: " . $file . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
  exec($cmd);

I tried just using the $_FILES information but that doesn't work. 我尝试仅使用$ _FILES信息,但这不起作用。

$cmd = "C:\Users\DJ LX\Documents\blender.old\blender.exe" $_FILES["file"]["type"];

and

$cmd = "C:\xampp\htdocs\test.new\upload" . $_FILES["file"]["type"];

I've also tried using fopen assigned as $openfile, then using exec($openfile), but an error appears stating that the 1st parameter in exec() needs to be a string. 我也尝试过使用分配为$ openfile的fopen,然后使用exec($ openfile),但是出现错误,指出exec()中的第一个参数需要为字符串。

$openfile = fopen($_FILES["file"]["type"], "r");

if ((($_FILES["file"]["type"] == "application/netfabb")
|| ($_FILES["file"]["type"] == "application/sla")
|| ($_FILES["file"]["type"] == "fileapplication/sla")
|| ($_FILES["file"]["type"] == "fileapplication/vnd.ms-pki.stl")))
  {
  if ($_FILES["file"]["error"] > 0)
   {
   echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
   }
  else
  {
  echo "Upload: " . $file . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
  exec($opefile);

Anyway, I'm really confused on how I could take the file name from the file uploaded and use it to open the file. 无论如何,我对如何从上传的文件中获取文件名并使用它来打开文件感到非常困惑。 Any help would be greatly appreciated. 任何帮助将不胜感激。

You're probably looking for $_FILES['file']['tmp_name'] . 您可能正在寻找$ _FILES ['file'] ['tmp_name']

Update 更新资料

A file is uploaded with a temporary(almost random) name. 使用临时(几乎随机)名称上传文件。 You get that with $_FILES['file']['tmp_name'] . 您可以通过$ _FILES ['file'] ['tmp_name']来实现 The file name of the source file is in $_FILES['file']['name'] . 源文件的文件名在$ _FILES ['file'] ['name']中 Your command line string needs a space between the executable and the file name and should probably look something like: 您的命令行字符串在可执行文件和文件名之间需要一个空格,并且可能看起来像这样:

$cmd = '"C:\Users\DJ LX\Documents\blender.old\blender.exe" '
           . escapeshellarg( $_FILES["file"]["tmp_name"] );

It may be safe with or without the double quotes - check that for yourself. 带或不带双引号可能是安全的-请自己检查。

Okay so I figured it out, thanks for the suggestion blue. 好吧,我明白了,谢谢蓝色的建议。 After playing with the $_FILES['file']['tmp_name'], I was able to get blender to attempt opening it but since it was unrecognized, (tmp file) it didn't work. 在玩完$ _FILES ['file'] ['tmp_name']之后,我能够使Blender尝试打开它,但是由于无法识别它(tmp文件),所以它不起作用。

I then reverted back to $_FILES["file"]["type"] but moved the exec($cmd) further down in the script, after the $_FILES["file"]["type"] is moved to the upload folder. 然后,我将$ _FILES [“ file”] [“ type”]移至上载后,又恢复为$ _FILES [“ file”] [“ type”],但在脚本中将exec($ cmd)移到了更下方夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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