move_uploaded_file / No such file or directory in PHP - php

i have a problem with the move_uploaded_file function this is the problem:
Warning: move_uploaded_file(/imagenes/Icon.png) [function.move-uploaded-file]: failed to >open stream: No such file or directory in /home/decc98/public_html/php/insert.php on line 6
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpIBBh5U' >to '/imagenes/Icon.png' in /home/decc98/public_html/php/insert.php on line 6
Insercion exitosa
Other stuff, i speak spanish so part of my code is in spanish... Anyways, my code is:
<?php
include "conexion.php";
$ruta = "/imagenes";
$archivo = $_FILES['imagen']['tmp_name'];
$nombreArchivo = $_FILES['imagen']['name'];
move_uploaded_file($archivo,$ruta."/".$nombreArchivo);
$ruta=$ruta."/".$nombreArchivo;
$texto = $_POST['descripcion'];
$id = rand(1,200);
$insertar = mysql_query("INSERT INTO tablaUno VALUES('".$id."','".$ruta."','".$texto."')");
if ($insertar) {
echo "Inserción exitosa";
}else{
echo "Fallo en la inserción";
}
?>
Please if anyone can help me I would appreciate it!

You need to use a relative path instead of an absolute path.
For example:
$ruta = "imagenes";
leaving out the / at the beginning of your folder name, if you're using your script from the root.
Or, something like:
$ruta = "../imagenes";
depending on the script execution's location.
Note: Using / is mostly used for an (server) absolute path, something to the affect of:
/var/user/user123/public_html/imagenes

Related

Copy() function can't detect destination path php [duplicate]

Anyone know why this:
<?PHP
$title = trim($_POST['title']);
$description = trim($_POST['description']);
// Array of allowed image file formats
$allowedExtensions = array('jpeg', 'jpg', 'jfif', 'png', 'gif', 'bmp');
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '') {
if (!in_array(end(explode(".",
strtolower($file['name']))),
$allowedExtensions)) {
echo '<div class="error">Invalid file type.</div>';
}
}
}
if (strlen($title) < 3)
echo '<div class="error">Too short title</div>';
else if (strlen($description) > 70)
echo '<div class="error">Too long desccription.</div>';
else {
move_uploaded_file($_FILES['userfile']['tmp_name'], 'c:\wamp\www\uploads\images/');
}
Gives:
Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in C:\wamp\www\upload.php on line 41
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php1AB.tmp' to 'c:\wamp\www\uploads\images/' in C:\wamp\www\upload.php on line 41
It's because you're moving a file and it thinks you're trying to rename that file to the second parameter (in this case a directory).
it should be:
move_uploaded_file($_FILES['userfile']['tmp_name'], 'c:/wamp/www/uploads/images/'.$file['name']);
You are specifying to move a file to a directory; neither PHP's move_uploaded_file nor its copy is as smart as a shell's copy -- you have to specify a filename, not a directory, for the destination.
So, one simple solution would be to take the basename of the source file and append that to the destination directory.
It sounds like the second argument to move_uploaded_file should be a full file name instead of just the directory name. Also, probably only a style issue, but you should use consistent slashes in 'c:\wamp\www\uploads\images/'
Because PHP is not a shell. You're trying to copy the file into the c:\wamp\www\uploads\images directory, but PHP doesn't know you mean that when you execute (within the move_uploaded_file function):
copy($_FILES['userfile']['tmp_name'], 'c:\wamp\www\uploads\images/');
This command tells it to rename the file to c:\wamp\www\uploads\images/, which it can't do because that's the name of an existing directory.
Instead, do this:
move_uploaded_file($_FILES['userfile']['tmp_name'],
'c:\wamp\www\uploads\images/' . basename($_FILES['userfile']['tmp_name']));
if you want just copy the file in two Dir different, try this :
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target.$pic1))
{
copy("C:/Program Files (x86)/EasyPHP-5.3.9/www/.../images/".$pic1, "C:/Program Files (x86)/EasyPHP-5.3.9/www/.../images/thumbs/".$pic1)
}
You should write the complete path "C:/..."
Try adding the extension to the name file.
$filename = $_FILES['userfile']['tmp_name'].".jpg";
It will be like below in phalcon 3.42
if ($this->request->hasFiles() == true) {
// Print the real file names and sizes
foreach ($this->request->getUploadedFiles() as $file) {
//Print file details
echo $file->getName(), " ", $file->getSize(), "\n";
//Move the file into the application
$file->moveTo($this->config->application->uploadsDir.$file->getName());
}
}
FIRSTLY FIND YOUR CURRENT DIRECTORY echo getcwd(); IF YOU FIND THE CURRENT DIRECTORY SO MOVE THEM ANOTHER DIRECTORY
FOR EXAMPLE = echo getcwd(); these output is "your live directory name" like that -> /opt/lampp/htdocs/product and create new directory anywhere but your current directory help to go another directory ***
plz read carefully very thinking answer

Why can't php find this file?

I have an Apache/PHP site running on a Drobo5n in Linux.
utilities.php is in /Choir/inc
hitCounter.txt is in /Choir/etc/tbc
In utilities.php, we have the following line of code:
$hits = file_get_contents('../etc/tbc/hitCounter.txt');
Which produces this error:
Warning: file_get_contents(../etc/tbc/hitCounter.txt): failed to open
stream: No such file or directory in
/mnt/DroboFS/Shares/DroboApps/apache/www/Choir/inc/utilities.php
on line 6
This is my first time fiddling with PHP and I cannot figure out why it can't find the file. I've tried both single and double quotes around the path to no avail.
I know someones gonna ask for the complete code so here's the utilities.php file:
<?php
session_cache_limiter('private_no_expire');
session_start();
function getHitCount() {
$hits = file_get_contents('../etc/tbc/hitCounter.txt');
if (!isset ($_SESSION['beenHere'])) {
$hits = $hits + 1;
file_put_contents('../etc/tbc/hitCounter.txt', "$hits");
$_SESSION['beenHere'] = "Yes I have";
}
return $hits;
}
?>
1) Should explicit your file path. Hard to say in this case. We should have our root application folder.
If we follow the MVC pattern, we will get the root application folder easily.
For example https://github.com/daveh/php-mvc
I like something:
$file = APP_ROOT . '/etc/tbc/hitCounter.txt';
#APP_ROOT has the path /mnt/DroboFS/Shares/DroboApps/apache/www/Choir
2) Check file_exists
if (!file_exists($file)) {
//Throw error here
}
3) Check: is_readable
if (!is_readable($File)) {
......
}

cannot move uploaded files failed to open stream

Hello so i have a simple upload system in php and i want to upload my files to ftp server but when i try to it doesnt work i get these two errors:
Warning: move_uploaded_file(/userfiles/grega): failed to open stream: No such file or directory in /srv/disk3/1618233/www/netdisk.co.nf/upload.php on line 19
Warning: move_uploaded_file(): Unable to move '/tmp/phpVtApVM' to '/userfiles/grega' in /srv/disk3/1618233/www/netdisk.co.nf/upload.php on line 19
and there is folder userfiles/grega on the ftp server please help me out
the code:
<?php
require_once 'core/init.php';
if($_POST[submit]) {
$name = $_FILES['upload']['name'];
$temp = $_FILES['upload']['tmp_name'];
$type = $_FILES['upload']['type'];
$size = $_FILES['upload']['size'];
if($size <= 5000000){
$user = new User();
if(!$user->isLoggedIn()) {
Redirect::to('index.php');
}
$uploads_dir = '/userfiles';
$username = ($user->data()->username);
move_uploaded_file($temp,"$uploads_dir/$username");
Session::flash('home', '<h3>Datoteka je bila naložena!</h3>');
Redirect::to('mojprofil.php');
} else{
echo "Napaka!";
}
} else {
header("Location: mojprofil.php");
}
?>
You say this:
there is folder userfiles/grega
But the error says this:
move_uploaded_file(/userfiles/grega)
Those are two very (even if subtly) different paths. Note also where you define the path in your code:
$uploads_dir = '/userfiles';
The code is looking for a folder called userfiles in the root of the entire file system, not just in the website. Perhaps you meant to do this?:
$uploads_dir = 'userfiles';

moving uploaded file to a dynamic table in php

I'm trying to move the file that I uploaded using move_uploaded_file. Here are my variables:
$filename = $_FILES['File_file']['name'];
$folder_id = $_POST['File']['folder_id'];
$folder_name_result = $this->filemanager_model->getfoldername($folder_id);
$fileloc = $_FILES['File_file']['tmp_name'];
$folder_name = "";
foreach ($folder_name_result->result_array() as $row)
{$folder_name = $row['title'];}
$pathAndName = "filemanager/".$folder_name."/".$filename;
And the outputs of the variables:
$folder_name = Grrr
$pathAndName = filemanager/Grrr/cis.png
$fileloc = C:\xampp1.8\tmp\phpE21E.tmp
When I run the move_uploaded_file function, it generates an error where:
move_uploaded_file(filemanager/Grrr/cis.png): failed to open stream: No such file or directory
move_uploaded_file(): Unable to move 'C:\xampp1.8\tmp\php2565.tmp' to 'filemanager/Grrr/cis.png'
My filepaths:
/admin - base_url
/admin/filemanager/Grr - The folder I want it to save
/admin/application/controllers/- the path of my controllers
Is there something wrong with my code as to why it's not working?
I just had something wrong with the formatting of my file path. I had to include the exact path since I'm on localhost, so once I put this on the server I've got to change path to the one on my server.
$pathAndName = "C:\\xampp1.8/htdocs/cicubecms/admin/filemanager/".$cat_name."/".$folder_name."/".$filename;

move_uploaded_file() fails to open stream due to invalid argument - PHP

I have this code to move my uploaded file to a specific directory:
if (isset($_FILES["image"]["name"])){
if (!is_dir('pf/' . $uid)) {
mkdir('pf/' . $uid);
$large_image_location = "pf/" . $uid;
}else {
$large_image_location = "pf/" . $uid;
}
chmod ($large_image_location, 0777);
move_uploaded_file("$userfile_tmp", "$large_image_location/$userfile_tmp");
}
However that gives the following error:
( ! ) Warning: move_uploaded_file(pf/BfyhieniKJGGqTNm/C:\wamp\tmp\phpF08A.tmp) [function.move-uploaded-file]: failed to open stream: Invalid argument in C:\wamp\www\mingle\upload_dp.php on line 26
Any help on how to sort this out would be greatly appreciated!
This is 90% of your woes:
move_uploaded_file("$userfile_tmp", "$large_image_location/$userfile_tmp");
You using the moved to path at the beginning of the upload path. Try:
move_uploaded_file("$userfile_tmp", "$large_image_location/".$_FILES['image']['name']);
That should work better.
The error itself is pretty clear, pf/BfyhieniKJGGqTNm/C:\wamp\tmp\phpF08A.tmp is not a valid filename.
Don't change the contents of $_FILES[n]['tmp_name'] (or $userfile_tmp for that matter), since it will always contain the full path to the uploaded file.

Categories