|
-
Aug 19th, 2010, 08:42 AM
#1
Thread Starter
Lively Member
File Upload Problem
Hello Guys,
This is about file uploading. When i use the code below, it copies the file to the "storage" folder i manually created on the server. No problem on that.
Code:
<?php
// Your file name you are uploading
$filename = $HTTP_POST_FILES['ufile']['name'];
$file_ext = substr($filename, strripos($filename, '.'));
echo $file_ext;
$new_file_name='NewFile'.$file_ext;
echo $new_file_name;
$path= "storage/".$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";
//$new_file_name = new file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$new_file_name."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
}
else
{
echo "Error";
}
}
?>
Now here is my problem, After creating a folder and a subfolder based on a users input on a textbox, I applied the copy script above which i thought will also work but it did not copy the file to the folder i want to.
Code:
<?php
$FileTitle = $_POST['txtTitle'];
$Version = $_POST['txtVersion'];
$FullFileName = $_POST['txtCtrl'];
if(is_dir('Storage/'.$FileTitle)==1)
{
}
else
{
//Make Directory
$thisdir = getcwd();
$dir_path = $thisdir ."/storage/". $FileTitle;
if(mkdir($dir_path, 0777))
{
//echo $dir_path;
//Create the Version Folder
$dir_path = $dir_path ."/". $Version;
if(mkdir($dir_path, 0777))
{
$filename = $HTTP_POST_FILES['ufile']['name'];
$file_ext = substr($filename, strripos($filename, '.'));
$new_file_name=$FileTitle.$Version.$file_ext;
$path= "storage/". $FileTitle ."/". $Version."/" .$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";
}
else
echo "Error";
}
}
else
echo "Failed to create [". $dir_path ."] directory.";
}
else
echo "Failed to create [". $dir_path ."] directory.";
}
?>
The highlighted text is the function and it always goes to the "Error". Is there something i have done or something i have missed?
-
Aug 19th, 2010, 11:58 AM
#2
Re: File Upload Problem
try echoing out $path to make sure that $path is actually the valid path that you want it to be.
you're not being very consistent with the paths you're using; you use $thisdir to create the first directory, and then use $dir_path to create the second directory, but when you define $path you don't use either of these. $dir_path already holds the path (that you know is valid), so use that variable to create $path.
PHP Code:
$path = $dir_path . "/" . $new_file_name;
also, something to note is that you're checking if is_dir() returns the value "1," and that function actually returns boolean values. PHP will probably evaluate the expression is_dir(...) == 1 as true because you're not using the === operator, but you probably shouldn't do this. is_dir() is an expression by itself, so you can structure your IF statement like so:
PHP Code:
if(is_dir($path)){
you might also want to use the $_FILES superglobal array -- $HTTP_POST_FILES is deprecated.
-
Aug 19th, 2010, 06:01 PM
#3
Thread Starter
Lively Member
Re: File Upload Problem
Thank you for the reply. I will try to do what you have said.
-
Aug 19th, 2010, 06:50 PM
#4
Thread Starter
Lively Member
Re: File Upload Problem
hello again,
Now, even if i do this:
Code:
$dir_path = "storage/". $FileTitle;
if(mkdir($dir_path, 0777))
{
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $dir_path))
{
echo "Successful<BR/>";
}
else
echo "Error";
}
}
else
{
}
Creates the folder but it still doesnt copy the file on the directory.
Everytime i access the "storage" folder on my computer, (even if i set the read only property of the folder to false) it turns it back to read only.
Does anyone have any workaround on this?
-
Aug 19th, 2010, 08:18 PM
#5
Re: File Upload Problem
Does this help?
PHP Code:
$MAXIMUM_FILESIZE = 1024 * 200; // 200KB $MAXIMUM_FILE_COUNT = 10; // keep maximum 10 files on server //echo exif_imagetype($_FILES['Filedata']); if (isset($_FILES['sendfile'])){ if (move_uploaded_file($_FILES['sendfile']['tmp_name'], "./temporary/".$_FILES['sendfile']['name'])){ echo "The file has been saved as: " .$_FILES['sendfile']['name']; }else{ echo "Error! Could not upload file."; } }
sendfile is the name of the file upload field.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Aug 19th, 2010, 08:48 PM
#6
Thread Starter
Lively Member
Re: File Upload Problem
Thank you for replying nightwalker. I have solved this problem. The code is below: i hope this would help others.
Code:
$file_title = $_POST['txtTitle'];
$file_version = $_POST['txtVersion'];
$full_file_name = $_POST['txtCtrl'];
$file_name = $_FILES['ufile']['name'];
$file_ext = substr($file_name, strripos($file_name, '.'));
//Create Directory
$thisdir = getcwd();
$dir_path = $thisdir. "/storage/". $file_title;
if(mkdir($dir_path, 0777))
{
$dir_path = $dir_path ."/". $file_version;
if(mkdir($dir_path, 0777))
{
$dirok = true;
}
else
{
$dirok = false;
echo "Failed to create [". $dir_path ."] directory.";
}
}
else
echo "Failed to create [". $dir_path ."] directory.";
if($dirok)
{
$new_file_name=$full_file_name.$file_ext;
echo $dir_path.'<br/><br/>';
if($ufile !=none)
{
if(copy($_FILES['ufile']['tmp_name'], $dir_path."/".$new_file_name))
{
echo "Successful<BR/>";
echo "File Name :".$new_file_name."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
}
else
{
echo "Error";
}
}
}
Thank you.
-
Aug 19th, 2010, 09:04 PM
#7
Re: File Upload Problem
for the record, "none" is not something you can check against. if you want to see if a variable is empty, you must check if it is empty() or if it is an empty string:
PHP Code:
if(empty($ufile)){
// the variable is empty
}else{
// the variable is not empty
}
// OR:
if($ufile == ""){
// the variable is empty
}else{
// the variable is not empty
}
but, your issue was that you were trying to copy a file to a folder name (you weren't appending the new filename).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|