I am trying to add pictures to my form but I can't seem to get it working. I have looked through every resource I can find but I haven't been able to solve this issue. My folder is writable, exists. The syntax of move_uploaded_file(tmpfileName, fileDestination) is right too. Here is my code:
if($_FILES['thread_image']['name']!=""){
$fileExtBreak = explode('.', $_FILES['thread_image']['name']);
$fileExt = strtolower(end($fileExtBreak));
$allowed = array('jpg','jpeg','png','pdf');
if(in_array($fileExt, $allowed)){
$_SESSION['errors'] = $errors;
if($_FILES['thread_image']['error'] > 0){
$errors[] = 'Something went wrong when trying to upload your image.';
$_SESSION['errors'] = $errors;
header("Location:topic.php?id=". $_GET['id'] ."");
}else{
if($_FILES['thread_image']['size']>=10000000){
$errors[] = 'The file you are trying to upload is too big';
$_SESSION['errors'] = $errors;
header("Location:topic.php?id=". $_GET['id'] ."");
}else{
if(!is_dir('images')){
$errors[]='This shit doesn\'t exist';
}else if(!is_writable('images')){
$errors[]='This shit isn\'t writable';
}else{
$errors[]='It exists and should work!!!!';
}
$fileName = $date.'.'.$fileExt;
$filePath = 'images/'.$fileName;
$errors[]=$_FILES['thread_image']['tmp_name'];
$errors[]=$fileName;
$errors[]=$filePath;
if(move_uploaded_file($_FILES['thread_image']['tmp_name'], $filePath)){
$errors[]="success";
}else{
$errors[]="didn't work, sorry";
}
$errors[]=mysqli_error($link);
$_SESSION['errors'] = $errors;
$stmt = $link->prepare("UPDATE threads set thread_has_image='1' WHERE thread_date=?");
$stmt->bind_param("s",$date);
$result=$stmt->execute();
}
}
}else{
$errors[] = "You cannot upload files with that type";
$_SESSION['errors'] = $errors;
header("Location:topic.php?id=". $_GET['id'] ."");
}
}
And here is what it spits out:
1)checks whether the folder exists, 2)spits out the temporary file name 3)spits out my file name I created, 4)spits out the file Destination I created 5)checks whether move_uploaded_file worked
Here is my form:
<form class="makerContainer" method="post" enctype="multipart/form-data" action=<?php echo '"threadMaker_handler.php?id=' . $_GET[ 'id'] . '"'; ?>>
<fieldset class="good">
<div class="makerForm">
Thread Title:
<input type="text" class="title" title="ThreadTitle" name="thread_title" <?php echo 'value="' . $name . '"'; ?>>
</div>
<div class="makerForm">
Thread Description:
<textarea class="description" rows="8" cols="100" name="thread"><?php echo '' . $thread . ''; ?></textarea>
<div class="submitPanel">
<input type="file" name="thread_image"/>
<input type="submit" class="button buttonModal buttonColor" value="+ Create Thread">
</div>
</div>
</fieldset>
</form>