PHP SMB file upload causes 500 Internal Server Error
<p>When I try to upload a file on a local folder to an FTP server via SMB, the file gets uploaded, but the server returns a 500 Internal Server Error with the following message: </p>
<blockquote>
<p>Warning: fopen(File.xls): cannot open stream: No such file or directory</p>
</blockquote>
<p>This is my upload function: </p>
<pre class="brush:php;toolbar:false;">public function upload($fileToUpload, $targetPath = "") {
if (!empty($targetPath)) {
if (substr($targetPath, -1, 1) != '/') {
$targetPath .= "/";
}
}
$fileName = basename($fileToUpload);
$this->srvShare->put($fileToUpload, $targetPath . $fileName);
}</pre>
<p>In this case, <strong>$fileToUpload</strong> is something like 'File.xls'.
I've tried passing the entire path to the function but it still results in the same error.
The upload is successful... the file is already on the server, but the code cannot continue as it still results in a 500 Internal Server Error. </p>
<p>This is the put() function in smb NativeShare: </p>
<pre class="brush:php;toolbar:false;">/*** Upload local files
*
* @param string $source local file
* @param string $target target file
* @return bool
*
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException*/
public function put($source, $target) {
$sourceHandle = fopen($source, 'rb');
$targetUrl = $this->buildUrl($target);
$targetHandle = $this->getState()->create($targetUrl);
while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
$this->getState()->write($targetHandle, $data, $targetUrl);
}
$this->getState()->close($targetHandle, $targetUrl);
return true;
}</pre></p>