0

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;
    }
}
4
  • 2
    When you write a question about an error, always always include details of the error. Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. Commented Oct 4, 2015 at 23:14
  • make sure you $path="./_citylogo/logo/".$filename; is writable Commented Oct 4, 2015 at 23:19
  • @NishanthMatha Thank you, thank you, thank you, thank youuu. That was the issue! Damn permissions. Commented Oct 5, 2015 at 0:03
  • no worries. Happy to help! Commented Oct 5, 2015 at 0:06

2 Answers 2

2
$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.
}
Sign up to request clarification or add additional context in comments.

Comments

0

make sure you $path="./_citylogo/logo/".$filename; is writable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.