I want to create a PHP web to manage my files on server. So I install XAMPP at my server and run my program in it. Let say I install it at C: Drive
I can upload file to another Drive such as D: or E: using this code :
move_uploaded_file($_FILES['file']['tmp_name'], $drive.':/'.$cat.'/'.$catname.'/'.$newfilename)
$drive for my target drive, $cat for my category group, adn $catname for category name. I can upload file easily.
The problem is when I want to called the file on my PHP using these code:
<audio controls>
<source src="'.$query['a_drive'].':/'.$query['c_group'].'/'.$query['c_name'].'/'.$query['a_link'], 'r'.'" type="audio/'.$extension.'">
Your browser does not support the audio element.
</audio>
$query
is my query to get data from database (SELECT * bla bla
)
My browser can't open the files I want to open, it return that I'm not allowed to access the files:
Not allowed to load local resource:
Does anyone know how to solve this?
The problem is that you print the local path to the file on the server. However, when the browser opens the page it interprets the path local to the client and blocks the access to it. Plus the file is on the server and not on the client.
Either you configure your webserver in such way that the folder, in which the files are saved, is accessible via the webserver (virtual folder) and you print the url (and not the local file path) in the html code.
Or, you use a php script to open the file based on a get parameter on the server and print its content using the appropriate mime header. The link in the html file will point to this php file eith the appropriate parameter.
UPDATE: file.php - this outputs the file to the client
<?php
$filename=$_GET['filename']; //parameter identifying the file to be downloaded. Should not be direct path as in this simple example, rather than the id of the file's data stored in a database. You need to add that piece of code.
header('Content-Type: text/plain'); //set MIME type - you need to store this along your path to the file, you can get it from $_FILES['uploadedfilename']['type'] when a file is uploaded via php
header("Content-disposition: inline"); //advises the browser to open the file inside the browser
readfile($filename); //outputs the file
?>
HTML link:
<audio controls>
<source src="file.php?filename=yourfilename" type="...">
</audio>
Let me emphasize once more: use an id to reference a file, not a path.