I'm running my website on localhost , when I upload files the uploaded file stored in htdocs (local host file) so i want to change the location to my project assets file which is in other location than localhost files this is my code :
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
$image = $_POST['file'];
$name = $_POST['name'];
$file = base64_decode($image);
file_put_contents($name , $file);
echo 'upload is finished';
?>
CodePudding user response:
Note: I used this question to check how would OpenAI perform on a real world StackOverflow question. I'm posting the answer here as it actually looks like a legitimate solution.
It looks like you want to change the directory where uploaded files are stored on your localhost web server. To do this, you can use the move_uploaded_file()
function in PHP. This function allows you to move an uploaded file to a new location on the server.
Here's an example of how you can use this function to move the uploaded file to a new directory:
// define the new location for the uploaded file
$newLocation = '/path/to/your/project/assets/' . $name;
// move the uploaded file to the new location
if (move_uploaded_file($file, $newLocation)) {
echo 'File successfully moved to new location';
} else {
echo 'Failed to move file to new location';
}
In the code above, we first define the new location for the uploaded file by concatenating the file name with the path to your project's assets directory. Then, we use the move_uploaded_file()
function to move the file to the new location. If the function is successful, it will return true
; otherwise, it will return false
.
You can then use this new location to access the uploaded file in your project. I hope this helps! Let me know if you have any other questions.
CodePudding user response:
To change the location where the uploaded files are stored, you can use the "file_put_contents" function and specify the path to your project assets folder as the first argument:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
$image = $_POST['file'];
$name = $_POST['name'];
$file = base64_decode($image);
// specify the path to your project assets folder
$path = '/path/to/project/assets/';
file_put_contents($path . $name , $file);
echo 'upload is finished';
?>
Make sure that the specified path is correct and that your PHP script has permission to write to that location. You can also create the folder if it doesn't exist using the "mkdir" function.