thedigicraft / Atom.CMS

Atom.CMS
56 stars 52 forks source link

Can't make an normal file upload delete the image. #81

Closed creptor closed 6 years ago

creptor commented 9 years ago

I have view all the videos and now I'm starting to change and create some things, but I got stuck.

I get the following unlink error: Warning: unlink(/home/a4701346/public_html/userIcons/Array) [function.unlink]: No such file or directory in /home/a4701346/public_html/admin/config/query.php on line 119

My file is right the only problem is the query and I can't make it remove the old file (user icon)

query.php: (line 119 = $deleteFile...)

    case 'users':
        if(isset($_POST['submitted']) == 1) {
            $allOK= 1;
            $id = $_GET['id'];
            $username = mysqli_real_escape_string($dbc, $_POST['username']);
            $name = mysqli_real_escape_string($dbc, $_POST['name']);
            if($_POST['password'] != ''){
                if($_POST['password'] == $_POST['passwordv']) {
                    $pass = " password = SHA1('$_POST[password]'),";
                    $verify = true;
                } else {
                    $verify = false;
                }
            } else {    
                $verify = false;    
            }

            if(isset($id) && $id != '') {

                $action = 'updated';
                $q = "UPDATE users SET username = '$username', name = '$name', email = '$_POST[email]',$pass coins = '$_POST[coins]', value = '$_POST[value]' WHERE id = $id";
                $r = mysqli_query($dbc, $q);

            } else {

                $action = 'add';
                $q = "INSERT INTO users (username, name, email, password, coins, value) VALUES ('$username', '$name', '$_POST[email]', SHA1('$_POST[password]'), '$_POST[coins]', '$_POST[value]')";

                if($verify == true) {
                    $r = mysqli_query($dbc, $q);
                }
            }
            if(isset($_FILES['fileToUpload'])){
                $uploadOk = 1;
                $ds = DIRECTORY_SEPARATOR;
                $storeFolder = 'userIcons';
                $target_file = basename($_FILES["fileToUpload"]["name"]);
                $ext = pathinfo($target_file,PATHINFO_EXTENSION);
                $newname = time();
                $random = rand(100,999);
                $name = $newname.$random.'.'.$ext;
                $q = "UPDATE users SET img = 'userIcons/$name' WHERE id = $id";
                $r = mysqli_query($dbc, $q);
                $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

                echo $q.'
'; echo mysqli_error($dbc); if($check !== false) { $error .= "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { $error .= "File is not an image."; $uploadOk = 0; $allOK = 0; } if ($_FILES["fileToUpload"]["size"] > 500000) { $error .= "Sorry, your file is too large."; $uploadOk = 0; $allOK = 0; } // Allow certain file formats if($ext != "jpg" && $ext != "png" && $ext != "jpeg" && $ext != "gif" ) { $error .= "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; $allOK = 0; } if ($uploadOk == 0) { $error .= "Sorry, your file was not uploaded."; $allOK = 0; // if everything is ok, try to upload file } else { if (!empty($_FILES)) { $query = "SELECT img FROM users WHERE id = $id"; $result = mysqli_query($dbc, $query); $old = mysqli_fetch_assoc($result); $tempFile = $_FILES['fileToUpload']['tmp_name']; $targetPath = dirname(dirname(dirname(__FILE__))).$ds; $targetFile = $targetPath.$storeFolder.$ds.$name; move_uploaded_file($tempFile,$targetFile); if($old != 'images/user.jpg') { 119------------------------>$deleteFile = $targetPath.$storeFolder.$ds.$old; if(!is_dir($deleteFile)) { unlink($deleteFile); } } } } } if($allOK == 1){ $message = '
User was '.$action.'!
'; } else { if(isset($_POST['id']) != '') { $message = '
User could not be '.$action.' because: '.mysqli_error($dbc).'
'; } if($error != NULL){ $message = '
User could not be '.$action.' because: '.$error.'
'; } if($verify == false and !isset($_POST['id'])) { $message = '
Password fields empty and/or do not match.
'; } } } if(isset($_GET['id'])) { $opened = data_user($dbc, $_GET['id']); } break;

If you could help me it would be great :)