'How do you reconcile the path of a file inside mySQL with the domain name your browser puts in front of it, to view it?
I'm successfully uploading some files to a folder and saving the path in table.
If I display the file's path in an html table, hovering over it, the address on deck to be linked always has my domain.com/folder infront of the local path (which also is showing the domain name). So, it's domain/partialpath/domain_again/fullpath.
The $_POST and such works great. This is the related code to the file upload.
$currentDirectory = getcwd();
$uploadDirectory = "/files/";
$fileName = $_FILES['the_file']['name'];
$tmpName = $_FILES['the_file']['tmp_name'];
$fileSize = $_FILES['the_file']['size'];
$fileType = $_FILES['the_file']['type'];
$uploadPath = $currentDirectory . $uploadDirectory . $fileName;
$cleaned = urlencode($uploadPath);
move_uploaded_file($tmpName, $uploadPath);
$batch = ("INSERT INTO filings VALUES ($cleaned)");
I was using urlencode() because some files have spaces and characters in their names, and I cannot tell if that's interfering with the url path or not.
Is the best convention to use assign a variable the text string of the path? $path = "/blah/blah/blah"?
Also, since this is the first project like this I've done, I'm curious why the browser puts my domain ahead of every path when hovering.
Solution 1:[1]
I believe I have answered my own question.
I was trying to build into the table the full path, but found that if I included the href properly concatenated with the row value it produces well. Though, I had to ensure steps to rawurlencode() the file name as some have spaces and characters in them. I'm not sure if it was redudant to use both but it's handling the different file names now.
$cleaned
is what is put into the mySQL INSERT query in the value of file_actual.
$cleaned = rawurlencode($fileName);
$uploadPath = $currentDirectory . $uploadDirectory . preg_replace("/[^\w\-\.\s\h]/", '', basename($fileName));
move_uploaded_file($tmpName, $uploadPath);
<?php echo "myDomain.com/Docs/courtfilings/files/" . $result->file_actual; ?>><?php echo $result->file_description; ?></td>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jona303 |