Jump to content

Recommended Posts

Hey guys need some help with some code I have been playing with..Here is my problem I have this upload script that I made it works but I need to make some revisions to this and I am not 100 percent sure how to do it.

 

$target = "/usr/local/apache/sites/secure/parts/"; 
$target = $target . basename( $_FILES['uploaded']['name'],$partNumber) ; 
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
} 
else {
$errMsg .= 'An unexpected error has occured.  Please try again and contact the Information Management Systems Administrators if the problem persists.';	}

 

Here is my question I have created a place for this upload to go as you can see the $target, but how can I revise this to work in a way that indexes each upload by part number? For example if I upload the file test.txt to part number 15 could it create a subdirectory for the different partnumber files to go? Does that make sense? I want to have all files to go with there corresponding part number but if the subdirectory doesn't exist I would like it to be created.  Please help!!

Link to comment
https://forums.phpfreaks.com/topic/181177-upload-help/
Share on other sites

I did something similar to this a while ago where I would allow user to upload images to my site and it would create a folder with the username and all their images would be stored there.

 

I did this by using the php mkdir function which would create a folder. Unfortunaltly I can not find the code I used but I remember I had some code that would check to see if the directory excisted first and if so it would just place a file there if not it made the directory first then continued to upload the file. Here is the syntax on the mkdir function.

 

http://php.net/manual/en/function.mkdir.php

 

Also another useful function I remember I used when people where deleting files from their folder was the unlink function here is the link for that.

 

http://php.net/manual/en/function.unlink.php

 

 

Hope this gets you started I will look for the code I used before and when I fined it will post back if you still need help.

 

 

Link to comment
https://forums.phpfreaks.com/topic/181177-upload-help/#findComment-955892
Share on other sites

Thanks for the help, I took what you said to use and came up with this:

if (file_exists($fileDest))
mkdir ($fileDest .$partNumber . "$file",  $mode = 0777, $recursive = false);
else{
echo "Error occurred contact System Adminstrators!";
}

Now my problem is that the directory is created and the file is uploaded but the file doesn't go into the subdirectory. I have created a spot /usr/local/apache/sites/secure/parts/ which is $fileDest and the mkdir code creates the subdirectory inside the above. How do I merge the two together so that all uploads just go straight to the corresponding subdirectory? I know it is something very small that I am overlooking but I think Im getting closer to resolving this problem.  Any ideas of what I could do differently?

Link to comment
https://forums.phpfreaks.com/topic/181177-upload-help/#findComment-956247
Share on other sites

I would recommend that you research into string concatenation (Joining) then simply join the $fileDest variable to the main path which I would suggest holding in another variable and then using that as the upload location for the file and it should work that way.

 

You will also need to implement some kind of file verification to make sure the file is of the right file type and also some path verification so that the user will not be able to upload the file to a directory that you do not want them to.

Link to comment
https://forums.phpfreaks.com/topic/181177-upload-help/#findComment-956260
Share on other sites

Do you know where I could find sufficient documentation on this.  I have been searching but I have run across anything that solves my problem.  I am not 100 percent sure on this, I know there has to be something somewhere. I have been stuck on this for about three days. It's long overdue!!

Link to comment
https://forums.phpfreaks.com/topic/181177-upload-help/#findComment-956327
Share on other sites

there are tons of file upload tutorials on the internet. try doing a google search for one. BTW I would suggest doing something like

if(!is_dir("whatever")){
mkdir("watever");
}

 

I believe the function will return false if the dir already exists. This may not be terrible, but just so you know

Link to comment
https://forums.phpfreaks.com/topic/181177-upload-help/#findComment-956334
Share on other sites

Thanks for the help...I get this error now : move_uploaded_file(/usr/local/apache/sites/secure/parts/00001uploaded/) [function.move-uploaded-file]: failed to open stream: Is a directory in /var/www/dev.neoterichovercraft.com/neoteric/code/inventory/upload.php on line 65.  Does this mean that I dont have the right permissions set? Here is the code I have now:

	

$target = $fileDest . basename( $_FILES['uploaded']['name'] ) ; 
$ok=1; 
$dest = $fileDest . "$partNumber" . "$file/";
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $dest ))
{
echo "File uploaded!";
} 
else {
$errMsg .= 'An unexpected error has occured.  Please try again and contact the Information Management Systems Administrators if the problem persists.';

	}
}
if (file_exists($fileDest)){
mkdir ($fileDest .$partNumber . "$file/",  $mode = 0777, $recursive = false);
}
else{
echo 'Error occurred contact System Manager!';
}

if (!move_uploaded_file($_FILES['uploaded']['tmp_name'], $dest)){
move_uploaded_file($file, $dest);
}

 

This produce the failed to open stream error..Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/181177-upload-help/#findComment-958566
Share on other sites

Im researching move_uploaded_file on php.net and it says

If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

 

 

Above is the script that I created to move the uploaded file to $dest which is all laid out in this discussion forum.  Here are the errors that I get when I click submit on the upload form to submit the file test.txt..

 

Warning: move_uploaded_file(/usr/local/apache/sites/secure/parts/00001uploaded/) [function.move-uploaded-file]: failed to open stream: Is a directory in /var/www/dev.neoterichovercraft.com/neoteric/code/inventory/upload.php on line 66.

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phppVwRe2' to '/usr/local/apache/sites/secure/parts/00001uploaded/' in /var/www/dev.neoterichovercraft.com/neoteric/code/inventory/upload.php on line 66

 

Warning: mkdir() [function.mkdir]: File exists in /var/www/dev.neoterichovercraft.com/neoteric/code/inventory/upload.php on line 76

 

The last error messages doesn't really bother me that much as it is just letting me know that the directory for that given part number has been created.  I'm in need of some assistance.  Can someone tell me what minor detail I have overlooked to resolve this matter TODAY! I would really appreciate your assistance in handling this tasks.  Im out of ideas..

 

Thank you

 

Thomas Clark

Link to comment
https://forums.phpfreaks.com/topic/181177-upload-help/#findComment-960025
Share on other sites

It looks like you are moving the file twice.  move_uploaded_file() will MOVE the file from the temporary location that it was uploaded to, so it is not there the next time you call move_uploaded_file(), and so you get an error.  Also, you are creating a $target value, but not using it anywhere.

 

To streamline the code, I would do something like this (this is an example):

// You don't really need basename() here, the name only contains the filename (with extension) (I think)
$target = basename($_FILES['uploaded']['name']);

// Assuming $fileDest is the base directory for your images
// AND Assuming it already has the / on the end of it
$dest = $fileDest . $partNumber . '/';

// Make the PartNumber directory if it is not already there
if (!is_dir($dest)) {
  mkdir ($dest,  $mode = 0777, $recursive = false);
}

// Move the uploaded file
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $dest . $target ))
{
  echo "File uploaded!";
} else {
  $errMsg .= 'An unexpected error has occured.  Please try again and contact the Information Management Systems Administrators if the problem persists.';
}

You may have to check to see if the destination file ($dest . $target) already exists before you try to move it.  If it does, you may want to use unlink() to delete it.  Or perhas rename it to *.old (in case the move fails, you can then rename it back).

Link to comment
https://forums.phpfreaks.com/topic/181177-upload-help/#findComment-960071
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.