Secure File Name, Content, Permission, and Server Handling in PHP
This article explains how to safely process uploaded file names, sanitize file contents, set appropriate Linux permissions, and handle file storage in PHP, while also providing code examples and a brief announcement for a PHP training class.
File Name Handling – When the original name is not required, generate a random name and append a whitelist‑validated extension. The example code shows a whitelist array, extraction and validation of the file extension, truncation of the base name, replacement of dots, and reconstruction of the sanitized filename.
<code>$extension_white_list = ['jpg', 'pdf'];
$origin_file_name = 'xx/xxx/10月CPI同比上涨2.1%.php.pdf';
$extension = strtolower(pathinfo($origin_file_name, PATHINFO_EXTENSION));
if (!in_array($extension, $extension_white_list)) {
die('错误的文件类型');
}
$new_file_name = pathinfo($origin_file_name, PATHINFO_BASENAME);
$new_file_name = mb_substr($new_file_name, 0, mb_strlen($new_file_name) - 1 - mb_strlen($extension));
$new_file_name = mb_substr($new_file_name, 0, 20);
$new_file_name = str_replace('.', '_', $new_file_name);
$new_file_name = $new_file_name . '.' . $extension;
print_r($new_file_name); // 10月CPI同比上涨2_1%_php.pdf</code>File Content Handling – Changing a file’s extension does not remove embedded PHP code; an attacker can hide malicious code in image files. The article demonstrates using the Windows copy command to concatenate a PHP script to a JPEG, and provides a PHP routine that re‑draws an image to strip hidden code, noting its memory cost and potential distortion.
<code>Copy 1.jpg/b + test.php/a 2.jpg</code> <code>try {
$jpg = '包含php代码的.jpg';
list($width, $height) = getimagesize($jpg);
$im = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($jpg);
imagecopyresampled($im, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$target = '重绘后干净的图片.jpg';
imagejpeg($image, $target);
} finally {
isset($im) && is_resource($im) && imagedestroy($im);
isset($image) && is_resource($image) && imagedestroy($image);
}</code>File Permission Handling (Linux) – The article explains the meaning of read (r/4), write (w/2), and execute (x/1) bits for files and directories, and describes the three user classes (owner, group, others). It recommends setting uploaded directories to 0755 and uploaded files to 0644 to prevent execution of malicious files.
<code>mkdir($save_path, 0755, true);</code> <code>chmod($file, 0644);</code>File Server Handling – For simplicity, the author suggests using an OSS storage service to store uploaded files.
Course Announcement – At the end of the technical guide, there is a promotion for the "PHP Development Basics and Practice" online live class, including enrollment links, schedule, and contact information.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.