enjoping / DXFighter

A new DXF writer and reader in PHP which outputs AC1012 (R15) DXF files
BSD 3-Clause "New" or "Revised" License
42 stars 23 forks source link

read the file name and path from url #27

Open bolbol62 opened 3 months ago

bolbol62 commented 3 months ago

Hi, which part in code will let direct upload from url name and an extension like 'http://localhost:xyz?path to dxf file or url ' instead of selecting and uploading the file by a click

enjoping commented 3 months ago

Hey,

if I understand correctly you are trying to read the contents of an external file and want to use it as your DXF. The reading of files by the library is limited to local files. So if you want to use an external file you have to download it using for example php curl or file_get_contents and write it to a temporary file. This temporary file can then be used as the $readPath option in the DXFighter constructor.

Hope this helps. Greetings Joe

bolbol62 commented 3 months ago

Understood Thanks, while the dxf is sitting local, I wanted to show it to viewers without using the upload selection from their end by sending the direct url '[http://localhost:xyz?path]' which will land into the nominated local file directly

tavsec commented 3 months ago

@bolbol62 I also encountered a situation where I had to use the DXF file from the remote location. To achieve this, update the read() method in the DXFighter.php class and remove the

if (!file_exists($path) || !filesize($path)) {
   throw new \Exception('The path to the file is either invalid or the file is empty');
}

part from the start of the method. PHP file_get_contents() function is able to fetch the files from the remote location, but file_exists and filesize expect local file, not the remote one. This will work, and you can also add additional validation for the "downloaded" DXF, but this was not necessary in my case.

So the updated read() method looks like this:

private function read($path, $move = [0,0,0], $rotate = 0) {
  $content = file_get_contents($path);
  $lines = preg_split ('/$\R?^/m', $content);
  $values = [];
  for ($i = 0; $i + 1 < count($lines); $i++) {
    $values[] = [
      'key' => trim($lines[$i++]),
      'value' => trim($lines[$i])
    ];
  }
  $this->readDocument($values, $move, $rotate);
}

I hope this helps!