Error in move_uploaded_file when file uploading
Hello
I am using formit to upload image to server and create resource.
In formit2resource, I checked image file was uploaded in formit temp folder.
But, when move file to destination by move_uploaded_file function, an error was occured and temp image in temp folder was not moved.
I am a beginner on MODx, so I hope if someone can help me.
here is my formit2resource code.
I am using formit to upload image to server and create resource.
In formit2resource, I checked image file was uploaded in formit temp folder.
But, when move file to destination by move_uploaded_file function, an error was occured and temp image in temp folder was not moved.
I am a beginner on MODx, so I hope if someone can help me.
here is my formit2resource code.
<?php
$doc = $modx->getObject('modResource',array('id'=>$hook->getValue('resource_id')));
if (empty($doc)){
$doc = $modx->newObject('modResource');
$doc->set('createdby', $modx->user->get('id'));
}
else{
$doc->set('editedby', $modx->user->get('id'));
}
$allFormFields = $hook->getValues();
$modx->log(MODx::LOG_LEVEL_ERROR, 'all form fields'.print_r($allFormFields,true));
foreach ($allFormFields as $field=>$value)
{
if ($field !== 'spam' && $field !== 'resource_id'){
$doc->set($field, $value);
}
}
$doc->set('template', '1');
$doc->save();
foreach ($allFormFields as $field=>$value)
{
if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
{
/* handles checkboxes & multiple selects elements */
if (is_array($value)) {
$featureInsert = array();
while (list($featureValue, $featureItem) = each($value)) {
$featureInsert[count($featureInsert)] = $featureItem;
}
$value = implode('||',$featureInsert);
}
//$modx->log(MODx::LOG_LEVEL_ERROR, 'array value-'.$doc->get('id').'-'.$value);
$tv->setValue($doc->get('id'), $value);
$tv->save();
}
}
//$modx->log(modX::LOG_LEVEL_ERROR,'Start of script-');
// initialize output;
$output = true;
// get the current user name to for dicroty placement
$userName = $modx->user->get('username');
// valid extensions
$ext_array = array('jpg', 'jpeg', 'gif', 'png');
// create unique path for this form submission
$uploadpath = 'assets/userfiles/trash/';
//$uploadpath = 'idt-mdv1-y/assets/images/trash/' . $userName .'/';
// get full path to unique folder
$target_path = $modx->config['base_path'] . $uploadpath;
// get uploaded file names:
$submittedfiles = array_keys($_FILES);
//get exsisting user profile data for Profile_Photos
//$fields = $modx->user->getOne('Profile')->get('extended');
//$Photo_fields = $fields['trashimage'];
// loop through files
foreach ($submittedfiles as $sf) {
// Get Filename and make sure its good.
$filename = basename( $_FILES[$sf]['name'] );
// Get file's extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$ext = mb_strtolower($ext); // case insensitive
if($filename != '') {
// is this the right type of file?
if(in_array($ext, $ext_array)) {
//create file called the filename that has been sanitized
$filename = strtolower(preg_replace("/[^A-Za-z0-9.]+/i", "-", $filename));
// full path to new file
$myTarget = $target_path . $filename;
// temp file name
$tempname = $_FILES[$sf]['tmp_name'];
$modx->log(MODx::LOG_LEVEL_ERROR, 'temp name-'.$tempname);
$modx->log(MODx::LOG_LEVEL_ERROR, 'target path-'.$myTarget);
//URL path
$urlPath = $uploadpath . $filename;
$modx->log(MODx::LOG_LEVEL_ERROR, 'url path-'.$urlPath);
// create directory to move file into if it doesn't exist
if(!is_dir($target_path)){
mkdir($target_path, 0755, true);
$modx->log(MODx::LOG_LEVEL_ERROR, 'folder create');
}
if(file_exists($myTarget)) {
chmod($myTarget,0755); //Change the file permissions if allowed
unlink($myTarget); //remove the file
$modx->log(MODx::LOG_LEVEL_ERROR, 'file delete');
}
// is the file moved to the proper folder successfully?
if(move_uploaded_file($tempname, $myTarget)) {
$modx->log(MODx::LOG_LEVEL_ERROR, 'file moved');
// $hook->setValue($sf, $uploadpath . $filename);
// $modx->setPlaceholder($sf, $urlPath);
// place file path to TV
// if (!$doc->setTVValue($sf, $urlPath)) $modx->log(MODx::LOG_LEVEL_ERROR, 'Upload:Saving TV error');
$tv = $modx->getObject('modTemplateVar', array ('name'=>'trashimage'));
$tv->setValue($doc->get('id'), $urlPath);
$tv->save();
// set the permissions on the file
if (!chmod($myTarget, 0644)) { /*some debug function*/ }
} else {
// File not uploaded
$errorMsg = 'There was a problem uploading the file.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
}
}
else {
// File type not allowed
$errorMsg = 'Type of file not allowed.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
}
} // if no file, don't error, but return blank
else {
// is the file name empty (no file uploaded) and exsiting photofields empty
$hook->setValue($sf, '');
$hook->addError($sf, 'no file uploaded');
$doc->setTVValue($sf, ''); // insert empty value to TV
}
}
$doc->save();
return $output;
Комментарии: 3
This problem is solved.
I have checked permission of formit/tmp. It was 0755. So I could not moved file to destination.
I have checked permission of formit/tmp. It was 0755. So I could not moved file to destination.
Sorry, didn`t see your question.
Yes, files permissions is the first thing to check in such cases =)
Yes, files permissions is the first thing to check in such cases =)
Thanks a lot for your kind reply.
Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.