I am working on making a CMS at the minute. I have got redactor coded so that I can edit text sucessfully with it. However I am now working getting an image upload to work in the text editor and have come across a few problems which I have been stuck on for quite some time.
I have been able to get to the stage where I can choose the image to upload... and then when it uploads, the path of the image goes into the text editor correctly, however the image does not seem to write to the directory.
I am working locally, and have the directory set as /CMS/public/uploads/
Here is my upload function.
<?php
// files storage folder
$dir = '/CMS/public/uploads/';
$_FILES['file']['type'] = strtolower($_FILES['file']['type']);
if ($_FILES['file']['type'] == 'image/png'
|| $_FILES['file']['type'] == 'image/jpg'
|| $_FILES['file']['type'] == 'image/gif'
|| $_FILES['file']['type'] == 'image/jpeg'
|| $_FILES['file']['type'] == 'image/pjpeg')
{
// setting file's mysterious name
$file = $dir.$_FILES['file']['name'];
// copying
move_uploaded_file($_FILES['file']['name'], $file);
// displaying file
$array = array(
'filelink' => $file
);
echo stripslashes(json_encode($array));
}
?>
This code basically tells the file where to be copied to.
Does anyone have any Idea why this might not be copying the the directory?
Thanks
I guess this should work: change move_uploaded_file($_FILES['file']['name'], $file);
to move_uploaded_file($_FILES['file']['tmp_name'], $file);
the $_FILES['file']['tmp_name']
is the place where the temporary file is located and from there it is moved to the specified path
Changed your code a bit: (Hope this works)
<?php
$type = strtolower($_FILES['file']['type']);
$allowed_type = array('image/png', 'image/jpg', 'image/gif', 'image/jpeg', 'image/pjpeg');
if(in_array($type, $allowed_type)) {
// setting file's mysterious name
$file = $_FILES['file']['name'];
$dir = '/CMS/public/uploads/'.$file;
// copying
move_uploaded_file($_FILES['file']['tmp_name'], $dir);
// displaying file
$array = array(
'filelink' => $file
);
echo stripslashes(json_encode($array));
}
?>