Reputation: 47
there. I'm not very experienced with PHP, but I've been charged with modifying a site that is very heavy in PHP. Part of the functionality is uploading an image and having it display on the page. I have gotten the image name to write to the database and the image HTML to display when the database field is not empty. However, I cannot figure out how to get the image to save to the server. Any help would be greatly appreciated!
HTML:
<form action="<?php echo $editFormAction; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
<label for="logo">Primary Logo Upload:</label> <input type="file" id="logo" name="logo" class="fullWidth" ><br>
<input type="submit" value="Save Changes">
<input type="hidden" name="logo" value="<?php echo $row_RecordsetCity['logo']; ?>" />
</form>
PHP:
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$location = $_FILES['logo'];
$image = rand(1,2);
$image = $image * time();
$image = $image."-lp".".jpg";
$filename = $image;
$path="./_citylogo/logo/".$filename;
$tempname=$location['tmp_name'];
copy($tempname,$path);
if ($location['name'] == "") {
$_POST['logo'] = $_POST['logo'];
} else {
$_POST['logo'] = $image;
}
}
Upvotes: 0
Views: 1025
Reputation: 335
$tempname= $location['tmp_name'];
$name = $location['tmp_name'];
$folder_directory = 'foldername/';
$check_folder_exists = is_dir($folder_directory);
if(!$check_folder_exists){
mkdir($folder_directory , 0755, true);
//create folder if it doesn't exists.
//0755 - the image file can be access for the owner, while other can read and execute it.
}
$file_directory = $folder_directory.$name;
$move_temp_file = move_uploaded_file($tempname, $file_directory);
//note: This next part is optional.
if($move_temp_file){
chmod($file_directory, 644);
//chmod() change the image file so it can't to executable by other in your server incase some hacker, upload a bad image.
}
Upvotes: 2
Reputation: 6081
make sure you $path="./_citylogo/logo/".$filename;
is writable
Upvotes: 0