Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

apache - Change permissions of file uploaded by PHP

I have created a small scale CMS for a website I am working on and have a form that uploads image files to be used on the website. It uploads the files successfully but the permissions it sets do not allow the file to be viewed in a browser.

Here is my current PHP code to upload the files

$typepath = $_POST['filetype'];

$target_path = "../../images/uploads/".$typepath."/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "<p>The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded</p>
<p>To the directory:  <span style="font-weight:bold;">".substr($target_path, 6)."</span></p>";
} else{
    echo "There was an error uploading the file, please try again!";
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

PHP Manaual chmod http://php.net/manual/en/function.chmod.php

chmod("/somedir/somefile", 0755);

In context;

$typepath = $_POST['filetype'];

$target_path = "../../images/uploads/".$typepath."/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    chmod($target_path, 0755);
    echo "<p>The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded</p>
<p>To the directory:  <span style="font-weight:bold;">".substr($target_path, 6)."</span></p>";
} else{
    echo "There was an error uploading the file, please try again!";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...